javascript - Backbone.js: Passing value From Collection to each model -
i need pass value view each models inside collection, while initializing.
till collection can pass 'options' in backbone.collection constructor.
after this, there technique can pass 'options' each models inside collection?
var song = backbone.model.extend({ defaults: { name: "not specified", artist: "not specified" }, initialize: function (attributes, options) { //need some_imp_value accessible here }, }); var album = backbone.collection.extend({ model: song initialize: function (models, options) { this.some_imp_value = option.some_imp_value; } });
you can override "_preparemodel" method.
var album = backbone.collection.extend({ model: song initialize: function (models, options) { this.some_imp_value = option.some_imp_value; }, _preparemodel: function (model, options) { if (!(model instanceof song)) { model.some_imp_value = this.some_imp_value; } return backbone.collection.prototype._preparemodel.call(this, model, options); } });
now can @ attributes passed model in 'initialize' , you'll some_imp_value, can set on model appropriate..
Comments
Post a Comment