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

Instance Method Summary

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

Parameters:

  • prototype_properties (Object) the properties to add to the prototype
  • class_properties (Object) the properties to add to the class

Returns:

Constructor Details

- (ko.observable) constructor(model, options, view_model)

Used to create a new kb.ViewModel.

Parameters:

  • model (Model|ModelRef) the model to observe (can be null)
  • options (Object) the create options
  • view_model (Object) a view model to also set the kb.Observables on. Useful when batch creating observable on an owning view model.

Options Hash: (option):

  • internals (Array|String) an array of atttributes that should be scoped with an underscore, eg. name -> _name
  • requires (Array|String) an array of atttributes that will have kb.Observables created even if they do not exist on the Model. Useful for binding Views that require specific observables to exist
  • keys (Array|String) restricts the keys used on a model. Useful for reducing the number of kb.Observables created from a limited set of Model attributes
  • if (Object|Array|String) an array is supplied, excludes keys to exclude on the view model; for example, if you want to provide a custom implementation. If an Object, it provides options to the kb.Observable constructor.
  • path (String) the path to the value (used to create related observables from the factory).
  • store (kb.Store) a store used to cache and share view models.
  • factories (Object) a map of dot-deliminated paths; for example {'models.name': kb.ViewModel} to either constructors or create functions. Signature: {'some.path': function(object, options)}
  • factory (kb.Factory) a factory used to create view models.
  • options (Object) a set of options merge into these options using _.defaults. Useful for extending options when deriving classes rather than merging them by hand.

Returns:

  • (ko.observable) — the constructor returns 'this'

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

Returns:

  • (Model|ModelRef|void) — getter: the model whose attributes are being observed (can be null) OR setter: void

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

- (void) shareOptions()

Get the options for a new view model that can be used for sharing view models.