Question

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
});
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top