Base class for objects that provide named properties with optional getter/setter control and the ability to observe for property changes.
The class also provides the functionality to auto-magically manage getters and setters for class attributes/properties. Note though that expando properties (i.e. properties added to an instance but not in the prototype) are not supported.
Getters and Setters should follow the format of _setXxxAttr
or _getXxxAttr
where
the xxx is a name of the attribute to handle. So an attribute of foo
would have a custom getter of _getFooAttr
and a custom setter of _setFooAttr
.
Setters must save and announce the new property value by calling this._set("foo", val)
,
and getters should access the property value as this._get("foo")
.
- Source:
Examples
var MyClass = dcl(Stateful, { foo: "initial" });
var obj = new MyClass();
obj.observe(function(oldValues){
if ("foo" in oldValues) {
console.log("foo changed to " + this.foo);
}
});
obj.foo = bar;
// Stateful by default interprets the first parameter passed to
// the constructor as a set of properties to set on the widget
// immediately after it is created.
var MyClass = dcl(Stateful, { foo: "initial" });
var obj = new MyClass({ foo: "special"});
Classes
Show inheritedMethods
-
<protected> _get(name) → {*}
-
Internal helper for directly accessing an attribute value.
Directly get the value of an attribute on an object, bypassing any accessor getter. It is designed to be used by descendant class if they want to access the value in their custom getter before returning it.
Parameters:
Name Type Description name
string Name of property.
- Source:
Returns:
Value of property.
- Type
- *
-
<protected> _set(name, value)
-
Internal helper for directly setting a property value without calling the custom setter.
Directly change the value of an attribute on an object, bypassing any accessor setter. Also notifies callbacks registered via observe(). Custom setters should call
_set
to actually record the new value.Parameters:
Name Type Description name
string The property to set.
value
* Value to set the property to.
- Source:
-
mix(hash)
-
Set a hash of properties on a Stateful instance.
Parameters:
Name Type Description hash
Object Hash of properties.
- Source:
Example
myObj.mix({ foo: "Howdy", bar: 3 });
-
notifyCurrentValue(name)
-
Notify current value to observers. Handy to manually schedule invocation of observer callbacks when there is no change in value.
Parameters:
Name Type Description name
string The property name.
- Source:
-
observe(callback) → {module:decor/Stateful.PropertyListObserver}
-
Observe for change in properties. Callback is called at the end of micro-task of changes with a hash table of old values keyed by changed property. Multiple changes to a property in a micro-task is squashed .
Parameters:
Name Type Description callback
function The callback.
- Source:
Returns:
The observer that can be used to stop observation or synchronously deliver/discard pending change records.
Example
var stateful = new (dcl(Stateful, { foo: undefined, bar: undefined, baz: undefined }))({ foo: 3, bar: 5, baz: 7 }); stateful.observe(function (oldValues) { // oldValues is {foo: 3, bar: 5, baz: 7} }); stateful.foo = 4; stateful.bar = 6; stateful.baz = 8; stateful.foo = 6; stateful.bar = 8; stateful.baz = 10;
-
<protected> processConstructorParameters()
-
Called after Object is created to process parameters passed to constructor.
- Source: