Class: kb.Observable

Defined in: src/core/observable.coffee

Overview

Base class for observing model attributes.

Examples:

How to create a ko.CollectionObservable using the ko.collectionObservable factory.

var ContactViewModel = function(model) {
  this.name = kb.observable(model, 'name');
  this.number = kb.observable(model, { key: 'number'});
};
var model = new Contact({ name: 'Ringo', number: '555-555-5556' });
var view_model = new ContactViewModel(model);

How to create a kb.Observable with a default value.

var model = Backbone.Model({name: 'Bob'});
var name = kb.observable(model, {key:'name', default: '(none)'}); // name is Bob
name.setToDefault(); // name is (none)

Instance Method Summary

Constructor Details

# (ko.observable) constructor(model, key_or_info, options, _vm = {})

Note: the constructor does not return 'this' but a ko.observable

Used to create a new kb.Observable.

Parameters:

  • model ( Model ) the model to observe (can be null)
  • options ( String|Array|Object ) the create options. String is a single attribute name, Array is an array of attribute names.

Options Hash: (options):

  • key ( String ) the name of the attribute.
  • read ( Function ) a function used to provide transform the attribute value before passing it to the caller. Signature: read()
  • write ( Function ) a function used to provide transform the value before passing it to the model set function. Signature: write(value)
  • args ( Array ) arguments to pass to the read and write functions (they can be ko.observables). Can be useful for passing arguments to a locale manager.
  • localizer ( Constructor ) a concrete kb.LocalizedObservable constructor for localization.
  • default ( Data|ko.observable ) the default value. Can be a value, string or ko.observable.
  • 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.
  • 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 does not return 'this' but a ko.observable

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

# (kb.CollectionObservable|kb.ViewModel|ko.observable) value()

Returns:

  • ( kb.CollectionObservable|kb.ViewModel|ko.observable ) — exposes the raw value inside the kb.observable. For example, if your attribute is a Collection, it will hold a CollectionObservable.

# (kb.TYPE_UNKNOWN|kb.TYPE_SIMPLE|kb.TYPE_ARRAY|kb.TYPE_MODEL|kb.TYPE_COLLECTION) valueType()

Returns:

  • ( kb.TYPE_UNKNOWN|kb.TYPE_SIMPLE|kb.TYPE_ARRAY|kb.TYPE_MODEL|kb.TYPE_COLLECTION ) — provides the type of the wrapped value.

    Quickly fuzzy find classes, mixins, methods, file:

    Control the navigation frame:

    You can focus and blur the search input: