Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch in backbone model with id

Tags:

backbone.js

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().

like image 993
SET Avatar asked Dec 05 '11 20:12

SET


1 Answers

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();
like image 105
SET Avatar answered Nov 20 '22 21:11

SET