简体   繁体   English

骨干网和codeigniter其余服务器问题

[英]backbone and codeigniter rest server issues

My frontend is backbone and frontend is codeigniter with Phil Sturgeon's REST Controller. 我的前端是骨干,前端是Phil Sturgeon的REST Controller的代码点火器。

I have one model: Publisher and one collection: Publishers 我有一个模型:Publisher,一个集合:Publishers

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

my RestPublisher controller has: 我的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');
}

The problem is that on this.model.save(); 问题是在this.model.save();上。 the url that is fired is: http://pubhub.local/restpublisher/1111 where 1111 is the id. 触发的网址是: http://pubhub.local/restpublisher/1111 ,其中1111是ID。

problem is that i get 404. if i just simulate a put request to http://pubhub.local/restpublisher/ everything works fine and i guess i can get the params from request->put() 问题是我得到404。如果我只是模拟对http://pubhub.local/restpublisher/的放置请求, 一切正常,我想我可以从request-> put()中获取参数。

is there a way to solve this problem? 有办法解决这个问题吗?

question 2: can someone please explain me why the name of the action should start with index? 问题2:有人可以解释一下为什么动作名称应该以index开头吗? why can't i just write action: publishers_post that on save of the collection will get the data? 为什么我不能只写动作:publishers_post表示在保存集合后将获取数据?

Thanks a Lot! 非常感谢! Roy 罗伊

I'll start with the second question: I guess it's just easier to parse, and it'll just be boilerplate anyway, so why not index_? 我将从第二个问题开始:我想它更容易解析,而且无论如何都是样板,所以为什么不使用index_?

Now, to go on with the interesting part. 现在,继续有趣的部分。 If your work with models inside a collection, Backbone, as a default behavior, will use the collection's URL to build a default one for your model ( collection.url/model.id ). 如果您使用集合中的模型,作为默认行为,Backbone将使用集合的URL为您的模型构建一个默认模型( collection.url / model.id )。 However, you can override this by setting a value for your model's URL: url: '/restpublisher' . 但是,您可以通过为模型的URL设置一个值来覆盖它: url: '/restpublisher'

Source 资源

Edit: 编辑:
To give you the general idea of how it works, it's easier to quote Backbone's Model#url code: 为了让您大致了解其工作原理,引用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);
}

So basically, if you override the url attribute in your model, this function will be erased, and the behavior you don't want as well. 因此,基本上,如果您在模型中覆盖url属性,则该功能将被删除,并且您也不需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM