2

我的前端是主干,前端是带有 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:有人可以解释一下为什么动作的名称应该以index开头吗?为什么我不能只写操作:publishers_post 保存集合时会获取数据?

多谢!罗伊

4

1 回答 1

1

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

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

来源

编辑:
为了让您大致了解它的工作原理,引用 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模型中的属性,这个函数将被删除,你不想要的行为也会被删除。

于 2013-04-08T12:09:07.793 回答