我的前端是主干,前端是Phil Sturgeon的REST控制器的编码器。
我有一个模型: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上保存的集合将得到数据?
谢谢你!罗伊
我将从第二个问题开始:我猜它只是更容易解析,它只是样板文件,那么为什么不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
属性,这个函数将被擦除,以及你不想要的行为。