Class: kb.ViewModel
| Defined in: | src/knockback-core/knockback-view-model.coffee |
Overview
Base class for ViewModels for Models.
Examples:
How to create a ViewModel with first_name and last_name observables.
var view_model = kb.viewModel(new Backbone.Model({first_name: "Planet", last_name: "Earth"}));
Bulk kb.Observable create using 'key' Object to customize the kb.Observable created per attribute.
var ContactViewModel = function(model) {
this.loading_message = new kb.LocalizedStringLocalizer(new kb.LocalizedString('loading'));
this._auto = kb.viewModel(model, {
keys: {
name: { key: 'name', 'default': this.loading_message },
number: { key: 'number', 'default': this.loading_message },
date: { key: 'date', 'default': this.loading_message, localizer: kb.ShortDateLocalizer }
}
}, this);
return this;
};
Creating ko.Observables on a target ViewModel
var view_model = {};
kb.viewModel(model, ['name', 'date'], view_model); // observables are added to view_model
Class Method Summary
- + (kb.ViewModel) extend(prototype_properties, class_properties) Class method for JavaScript inheritance.
Instance Method Summary
- - (Model|ModelRef|void) model() Dual-purpose getter/setter ko.dependentObservable/ko.computed for the observed model.
- - (ko.observable) constructor(model, options, view_model) Constructor Used to create a new kb.ViewModel.
- - (void) destroy() Required clean up function to break cycles, release view models, etc.
- - (void) shareOptions() Get the options for a new view model that can be used for sharing view models.
Class Method Details
+ (kb.ViewModel) extend(prototype_properties, class_properties)
Class method for JavaScript inheritance.
Examples:
var ContactViewModel = kb.ViewModel.extend({
constructor: function(model) {
kb.ViewModel.prototype.constructor.call(this, model, {internals: ['email', 'date']}); // call super constructor: @name, @_email, and @_date created in super from the model attributes
this.email = kb.defaultObservable(this._email, 'your.name@yourplace.com');
this.date = new LongDateLocalizer(this._date);
return this;
}
});
var ViewModel = kb.ViewModel.extend({
constructor: function(model){
kb.ViewModel.prototype.constructor.apply(this, arguments);
this.full_name = ko.dependentObservable(function() { return this.first_name() + " " + this.last_name(); }, this);
}
});
var view_model = new ViewModel(model);
Constructor Details
- (ko.observable) constructor(model, options, view_model)
Used to create a new kb.ViewModel.
Instance Method Details
- (Model|ModelRef|void) model()
Dual-purpose getter/setter ko.dependentObservable/ko.computed for the observed model.
Examples:
var view_model = kb.viewModel(new Backbone.Model({name: 'bob'}));
var the_model = view_model.model(); // get
view_model.model(new Backbone.Model({name: 'fred'})); // set
- (void) destroy()
Required clean up function to break cycles, release view models, etc. Can be called directly, via kb.release(object) or as a consequence of ko.releaseNode(element).
Get the options for a new view model that can be used for sharing view models.