collection.modelId

modelIdcollection.modelId(attrs)
Override this method to return the value the collection will use to identify a model given its attributes. Useful for combining models from multiple tables with different idAttribute values into a single collection.

By default returns the value of the attributes' idAttribute from the collection's model class or failing that, id. If your collection uses a model factory and those models have an idAttribute other than id you must override this method.

var Library = Backbone.Collection.extend({
  modelId: function(attrs) {
    return attrs.type + attrs.id;
  }
});

var library = new Library([
  {type: 'dvd', id: 1},
  {type: 'vhs', id: 1}
]);

var dvdId = library.get('dvd1').id;
var vhsId = library.get('vhs1').id;
alert('dvd: ' + dvdId + ', vhs: ' + vhsId);
doc_Backbone
2016-04-17 12:21:25
Comments
Leave a Comment

Please login to continue.