I have a list of items and clicking on them should bring to user details on it (another view). In my router I have such a route for this:
'details/:id' : 'details',
Now, in details function I should fetch my object (Fetching will trigger new view rendering). But what should i do so that backbone attach item id to model.url? 2 ways i have for now is:
Create url on the fly when fetching:
this.realty_object.fetch({url: SITE_PATH + 'blah/blah/' + id});
And something like this:
this.realty_object
.set({id : id}, {silent: true})
.fetch();
But I have doubts that it is right solutions. How it should be? What is the difference between model.urlRoot and model.url().
The right answer is using urlRoot property of model. If it is set up, models id will be appended to this url while fetching. So it may look like this (objects have different names from what is in my first post but idea is the same - fetch single model by id):
Group = Backbone.Model.extend({
urlRoot: SITE_PATH + 'group/index',
});
And in route:
this.group = new Group({id: id});
this.group.fetch();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With