Class: kb.ViewModel

Defined in: src/core/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 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

Instance Method Summary

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: (options):

  • 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
  • excludes ( Object|Array|String ) if 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.
  • statics ( Array ) creates non-observable properties on your view model for Model attributes that do not need to be observed for changes.
  • static_defaults ( Object ) provides default values for statics.
  • 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. Useful for extending options when deriving classes rather than merging them by hand.

Returns:

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

Instance Method Details

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

# (void) createObservables(model, keys)

create observables manually

    Quickly fuzzy find classes, mixins, methods, file:

    Control the navigation frame:

    You can focus and blur the search input: