简体   繁体   中英

backbone and codeigniter rest server issues

My frontend is backbone and frontend is codeigniter with Phil Sturgeon's REST Controller.

I have one model: Publisher and one collection: Publishers

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

my RestPublisher controller has:

 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(); the url that is fired is: http://pubhub.local/restpublisher/1111 where 1111 is the 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()

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? why can't i just write action: publishers_post that on save of the collection will get the data?

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_?

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 ). However, you can override this by setting a value for your model's 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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