Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get /collection/id in backbone without loading the entire collection

Is there a way to load a single entity of a Backbone collection (from the server)?

Backbone.Collection.extend({
  url: '/rest/product'
});

The following code can load the entire collection with a collection.fetch() but how to load a single model? Backbone's documentation says clearly that GET can be done /collection[/id] but not how.

like image 327
yves amsellem Avatar asked Jun 07 '11 08:06

yves amsellem


1 Answers

The model has to be declared that way:

Backbone.Model.extend({
  url: function() {
    return '/rest/product/'+this.id;
  }
});

Using it is simple as:

var model = new ProductModel();
model.id = productId;
model.fetch({ success: function(data) { alert(JSON.stringify(data))}});
like image 172
yves amsellem Avatar answered Oct 01 '22 01:10

yves amsellem