dojo/_base/kernel.Stateful

Summary

Base class for objects that provide named properties with optional getter/setter control and the ability to watch for property changes

The class also provides the functionality to auto-magically manage getters and setters for object attributes/properties.

Getters and Setters should follow the format of _xxxGetter or _xxxSetter where the xxx is a name of the attribute to handle. So an attribute of "foo" would have a custom getter of _fooGetter and a custom setter of _fooSetter.

Examples

Example 1

require(["dojo/Stateful", function(Stateful) {
    var obj = new Stateful();
    obj.watch("foo", function(){
        console.log("foo changed to " + this.get("foo"));
    });
    obj.set("foo","bar");
});

Properties

Methods

get(name)

Defined by dojo/Stateful

Get a property on a Stateful instance.

Get a named property on a Stateful object. The property may potentially be retrieved via a getter method in subclasses. In the base class this just retrieves the object's property.

Parameter Type Description
name String

The property to get.

Returns: any | undefined

The property value on this Stateful instance.

Examples

Example 1

require(["dojo/Stateful", function(Stateful) {
    var stateful = new Stateful({foo: 3});
    stateful.get("foo") // returns 3
    stateful.foo // returns 3
});

postscript(params)

Defined by dojo/Stateful

Parameter Type Description
params Object
Optional

set(name,value)

Defined by dojo/Stateful

Set a property on a Stateful instance

Sets named properties on a stateful object and notifies any watchers of the property. A programmatic setter may be defined in subclasses.

Parameter Type Description
name String

The property to set.

value Object

The value to set in the property.

Returns: any | function

The function returns this dojo.Stateful instance.

Examples

Example 1

require(["dojo/Stateful", function(Stateful) {
    var stateful = new Stateful();
    stateful.watch(function(name, oldValue, value){
        // this will be called on the set below
    }
    stateful.set(foo, 5);

set() may also be called with a hash of name/value pairs, ex:

stateful.set({
    foo: "Howdy",
    bar: 3
});
});

This is equivalent to calling set(foo, "Howdy") and set(bar, 3)

watch(name,callback)

Defined by dojo/Stateful

Watches a property for changes

Parameter Type Description
name String
Optional

Indicates the property to watch. This is optional (the callback may be the only parameter), and if omitted, all the properties will be watched

callback Function

The function to execute when the property changes. This will be called after the property has been changed. The callback will be called with the |this| set to the instance, the first argument as the name of the property, the second argument as the old value and the third argument as the new value.

Returns: any | object

An object handle for the watch. The unwatch method of this object can be used to discontinue watching this property:

var watchHandle = obj.watch("foo", callback);
watchHandle.unwatch(); // callback won't be called now

© 2005–2017 JS Foundation
Licensed under the AFL 2.1 and BSD 3-Clause licenses.
http://dojotoolkit.org/api/1.10/dojo/_base/kernel.Stateful.html