Backbone-ModelRef.js provides a mechanism to respond to lazy-loaded Backbone.js models.
window.ThingCellViewModel = kb.ViewModel.extend({
constructor: function(model, options) {
var _this = this;
kb.ViewModel.prototype.constructor.call(this, model, {
requires: ['id', 'name', 'caption', 'my_things', 'my_owner'],
factories: {
'my_things': ThingCellCollectionObservable,
'my_owner': ThingLinkViewModel
},
options: options
});
// use the parity interface between Backbone.Model and Backbone.ModelRef can be used to easily set up a loading state
this.is_loaded = ko.observable(model && model.isLoaded());
this._onModelLoaded = function(_model) {
_this.start_attributes = _model.toJSON();
return _this.is_loaded(true);
};
!model || model.bindLoadingStates(this._onModelLoaded);
return this;
}
});