【问题标题】:backbone and codeigniter rest server issues骨干网和codeigniter休息服务器问题
【发布时间】:2013-04-08 11:11:19
【问题描述】:

我的前端是主干,前端是带有 Phil Sturgeon 的 REST 控制器的 codeigniter。

我有一个模型:Publisher 和一个集合:Publishers

App.Collections.Publishers = Backbone.Collection.extend({
    model: App.Models.Publisher,
    url: '/restpublisher'
});

我的 RestPublisher 控制器有:

 function index_post(){
    $this->response('in pulisher_post');

}

function index_delete(){
    $this->response('in pulisher_delete');

}

function index_put(){
    $this->response('in pulisher_put');
}

问题在于 this.model.save();触发的 url 是:http://pubhub.local/restpublisher/1111 其中 1111 是 id。

问题是我得到 404。如果我只是模拟对 http://pubhub.local/restpublisher/ 的放置请求,一切正常,我想我可以从 request->put() 获取参数

有没有办法解决这个问题?

问题2:谁能解释一下为什么action的名字应该以index开头? 为什么我不能只写操作:publishers_post 保存集合时会获取数据?

非常感谢! 罗伊

【问题讨论】:

    标签: php codeigniter backbone.js


    【解决方案1】:

    我将从第二个问题开始:我想它更容易解析,而且它只是样板,所以为什么不 index_?

    现在,继续有趣的部分。如果您使用集合中的模型,Backbone 作为默认行为,将使用集合的 URL 为您的模型构建默认的 URL (collection.url/model.id)。但是,您可以通过为模型的 URL 设置一个值来覆盖它:url: '/restpublisher'

    Source

    编辑:
    为了让您大致了解它的工作原理,引用 Backbone 的 Model#url 代码更容易:

    url: function() {
      var base = _.result(this, 'urlRoot') || _.result(this.collection, 'url') || urlError();
      if (this.isNew()) return base;
      return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.id);
    }
    

    所以基本上,如果你在你的模型中覆盖url 属性,这个函数将被删除,你不想要的行为也会被删除。

    【讨论】:

    • Backbone 模型是从集合 url 继承的,因此它已经有 url:"/restpublisher"。这根本不是问题...问题是主干产生了 url pubhub.local/restpublisher/1111 其中 codeigniter 期望一个 url:pubhub.local/restpublisher 没有附加 id
    • 模型没有继承任何东西(我不确定术语 inherit 是否被改编)。如果模型中未指定,则在需要时动态评估 URL。如果您在此处指定一个,它将不再使用该集合的 URL。正是您需要的行为。
    • 我可以研究一下,但由于我从未使用过 CodeIgniter,我想这会很慢,因为我必须先了解 REST API 的机制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2011-03-24
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多