0

why use model inside collection, can someone explain me?

Example:

var model = Backbone.Model.extend({
  url: '/rest/article/'
});

var collection = Backbone.Collection.extend({
  url: '/rest/article',
  model: model
});
Mirza Delic
  • 4,119
  • 12
  • 55
  • 86
  • Possible duplicate of [Backbone.js Purpose of a Collection with a Model](http://stackoverflow.com/questions/27448398/backbone-js-purpose-of-a-collection-with-a-model) – T J Dec 14 '15 at 05:20

2 Answers2

1

So whenever you add an item to the collection it can be instantiated as that kind of model.

Decoy
  • 550
  • 4
  • 6
1
var model = Backbone.Model.extend({
  parse : function (response) {
    // you can modify model
    response.author || (response.author = 'Anonymous');
    return response;
  },
  getArticleTitle : function () {
    return this.get('author') + ' - ' + this.get('title');
  }
});

var collection = Backbone.Collection.extend({
  url: '/rest/article',
  model: model
});

// you can access model methods
collection.at(0).getArticleTitle();
Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55