Module: decor/Observable

decor/Observable

require(["decor/Observable"], function (Observable) {
  new Observable(o);
});

An observable object, working as a shim of ECMAScript Harmony Object.observe().

Parameters:
Name Type Description
o Object

The object to mix-into the new Observable.

Source:
Example
var observable = new Observable({foo: "Foo0"});
Observable.observe(observable, function (changeRecords) {
    // Called at the end of microtask with:
    //     [
    //         {
    //             type: "update",
    //             object: observable,
    //             name: "foo",
    //             oldValue: "Foo0"
    //         },
    //         {
    //             type: "add",
    //             object: observable,
    //             name: "bar"
    //         }
    //     ]
});
observable.set("foo", "Foo1");
observable.set("bar", "Bar0");

Classes

Notifier
Show inherited

Members

<inner, constant> DEFAULT_CHANGETYPES :Array.<module:decor/Observable~ChangeType>

The default list of change record types, which is: [ "add", "update", "delete", "reconfigure", "setPrototype", "preventExtensions" ]

Type:
Source:

Methods

<static> assign(dst, var_args) → {Object}

Copy properties of given source objects to given target object. If target object has set() function for the property, uses it.

Parameters:
Name Type Argument Description
dst Object

The target object.

var_args Object <repeatable>

The source objects.

Source:
Returns:

The target object.

Type
Object

<static> canObserve(o) → {boolean}

Parameters:
Name Type Description
o Object

The object to test.

Source:
Returns:

true if o can be observed with Observable.observe().

Type
boolean

<static> deliverChangeRecords(callback)

Delivers change records immediately.

Parameters:
Name Type Description
callback function

The change callback to deliver change records of.

Source:

<static> getNotifier(observable) → {module:decor/Observable~Notifier}

Obtains a notifier object for the given Observable.

Parameters:
Name Type Description
observable Object

The Observable to get a notifier object of.

Source:
Returns:
Type
module:decor/Observable~Notifier

<static> is() → {boolean}

Source:
Returns:

true if the given two values are the same, considering NaN as well as +0 vs. -0.

Type
boolean

<static> observe(observable, callback, accept) → {Handle}

If the 1st argument is non-object or null.

Parameters:
Name Type Argument Default Description
observable Object

The Observable to observe.

callback module:decor/Observable~ChangeCallback

The change callback.

accept Array.<module:decor/Observable~ChangeType> <optional>
module:decor/Observable~DEFAULT_CHANGETYPES

The list of change record types to observe.

Source:
Throws:
If the 1st argument is non-object or null.
Type
TypeError
Returns:

The handle to stop observing.

Type
Handle

<static> test(o) → {boolean}

Parameters:
Name Type Description
o Object

The object to test.

Source:
Returns:

true if o is an instance of Observable.

Type
boolean

set(name, value)

Sets a value. Automatically emits change record(s) compatible with Object.observe() if no ECMAScript setter is defined for the given property. If ECMAScript setter is defined for the given property, use Observable.getNotifier(observable).notify(changeRecord) to manually emit a change record.

Parameters:
Name Type Description
name string

The property name.

value

The property value.

Source:
Returns:

The value set.

Type Definitions

ChangeCallback(changeRecords)

Change callback.

Parameters:
Name Type Description
changeRecords Array.<module:decor/Observable~ChangeRecord>

The change records.

Source:

ChangeRecord

Change record seen in Observable.observe().

Type:
  • Object
Properties:
Name Type Argument Description
type module:decor/Observable~ChangeType

The type of change record.

object Object

The changed object.

name string <optional>

The changed property name. Set only for non-splice type of change records.

index number <optional>

The array index of splice. Set only for splice type of change records.

removed Array <optional>

The removed array elements. Set only for splice type of change records.

addedCount number <optional>

The count of added array elements. Set only for splice type of change records.

Source:

ChangeType

Change record type. One of:

  • "add"
  • "update"
  • "delete"
  • "reconfigure"
  • "setPrototype"
  • "preventExtensions"
  • "splice"
Type:
  • string
Source: