Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why specifying Model in Backbone Collection

What is the aim of specifying a model in a Backbone collection? It seems that the collection need its own url. Why do this:

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

Instead of:

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

With a model like this:

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

Is there a way to group url declaration?

like image 843
yves amsellem Avatar asked Feb 14 '26 02:02

yves amsellem


1 Answers

What is the aim of specifying a model in a Backbone collection

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

Basically your saying every model inside the collection is an instance of Model. It's also useful for doing this

col.add({
  prop1: "foo", 
  ...
});

And it will call new Model({prop1: "foo", ... }) for you and add it to the collection.

.model

like image 51
Raynos Avatar answered Feb 16 '26 21:02

Raynos