Attribute.js revision 3681658caa31e1b61b575d03cde5d5798b101eaa
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * Managed Attribute Provider
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @module attribute
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith var O = Y.Object,
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * Attribute provides managed attribute support.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * The class is designed to be augmented onto a host class,
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * and allows the host to support getter/setter methods for attributes,
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * initial configuration support and attribute change events.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <p>Attributes added to the host can:</p>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <li>Be defined as read-only.</li>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <li>Be defined as write-once.</li>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <li>Be defined with a setter function, used to manipulate
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * values passed to Attribute's set method, before they are stored.</li>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <li>Be defined with a validator function, to validate values before they are stored.</li>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <li>Be defined with a getter function, which can be used to manipulate stored values,
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * before they are returned by Attribute's get method.</li>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <p>See the <a href="#method_addAtt">addAttr</a> method, for details about how to add attributes with
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * a specific configuration</p>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @class Attribute
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @uses Event.Target
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith Y.log('Attribute constructor called', 'info', 'attribute');
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith // Perf tweak - avoid creating event literals if not required.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * Adds an attribute with the provided configuration to the host object. Intended
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * to be used by the host object to setup it's set of available attributes.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * The config argument object literal supports the following optional properties:
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dt>value <Any></dt>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dd>The initial value to set on the attribute</dd>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dt>readOnly <Boolean></dt>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dd>Whether or not the attribute is read only. Attributes having readOnly set to true
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * cannot be set by invoking the set method.</dd>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dt>writeOnce <Boolean></dt>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dd>Whether or not the attribute is "write once". Attributes having writeOnce set to true,
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * can only have their values set once, be it through the default configuration,
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * constructor configuration arguments, or by invoking set.</dd>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dt>setter <Function></dt>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dd>The setter function to be invoked (within the context of the host object) before
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * the attribute is stored by a call to the setter method. The value returned by the
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * setter function will be the finally stored value.</dd>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dt>getter <Function></dt>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dd>The getter function to be invoked (within the context of the host object) before
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * the stored values is returned to a user invoking the getter method for the attribute.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * The value returned by the getter function is the final value which will be returned to the
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * user when they invoke get.</dd>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dt>validator <Function></dt>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <dd>The validator function which is invoked prior to setting the stored value. Returning
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * false from the validator function will prevent the value from being stored</dd>
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @method addAttr
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {String} name The attribute key
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {Object} config (optional) An object literal specifying the configuration for the attribute.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * <strong>NOTE:</strong> The config object is modified when adding an attribute,
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * so if you need to protect the original values, you will need to merge or clone the object.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @chainable
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith Y.log('Adding attribute: ' + name, 'info', 'attribute');
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith if (config[READ_ONLY] && !hasValue) { Y.log('readOnly attribute: ' + name + ', added without an initial value. Value will be set on intial call to set', 'warn', 'attribute');}
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith // We'll go through set, don't want to set value in _conf directory
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith // Go through set, so that raw values get normalized/validated
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith Y.log('Attribute: ' + name + ' already exists. Cannot add it again without removing it first', 'warn', 'attribute');
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith return this;
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * Tests if the given attribute has been added to the host
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @method attrAdded
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {String} name The name of the attribute to check.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @return boolean, true if an attribute with the given name has been added.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * Removes an attribute.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @method removeAttr
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {String} name The attribute key
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * Returns the current value of the attribute. If the attribute
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * has been configured with a 'getter' function, this method will delegate
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * to the 'getter' to obtain the value of the attribute.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * The 'getter' will be passed the current value of the attribute
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * as the only argument.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @method get
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {String} name The attribute whose value will be returned. If
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * the value of the attribute is an Object, dot notation can be used to
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * obtain the value of a property of the object (e.g. <code>get("x.y.z")</code>)
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @return {Any} The current value of the attribute
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith val = (getter) ? getter.call(this, val, fullName) : val;
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * Sets the value of an attribute.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @method set
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @chainable
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {String} name The name of the attribute. Note, if the
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * value of the attribute is an Object, dot notation can be used
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * to set the value of a property within the object (e.g. <code>set("x.y.z", 5)</code>).
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {Any} value The value to apply to the attribute
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {Object} opts Optional event data. This object will be mixed into
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * the event facade passed as the first argument to subscribers
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * of attribute change events
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * @return {Object} Reference to the host object
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * Resets the given attribute or all attributes to the initial value, if the attribute
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * is not readOnly, or writeOnce.
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * @method reset
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * @param {String} name optional An attribute to reset. If omitted, all attributes are reset.
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * @chainable
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith this.set(name, this._conf.get(name, INIT_VALUE));
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith return this;
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * Allows setting of readOnly/writeOnce attributes.
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * @method _set
ce82477cabe9511f9b4db3793dc39704214058d3Luke Smith * @protected
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @chainable
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @return {Object} Reference to the host object
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * Internal set implementation
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @method _setAttr
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @protected
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @chainable
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @param {String} name The name of the attribute. Note, if the
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * value of the attribute is an Object, dot notation can be used
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * to set the value of a property within the object
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * (e.g. <code>set("x.y.z", 5)</code>).
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * @param {Any} value The value to apply to the attribute
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * @param {Object} opts Optional event data. This object will be mixed into
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * the event facade passed as the first argument to subscribers
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * of attribute change events
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * @param {boolean} force If true, allows the caller to set values for
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith * readOnly or writeOnce attributes which have already been set.
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith * @return {Object} Reference to the host object
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith initialSet = (!data.value || !(name in data.value));
14ea8dcb06b6d844dd99b7e18cca99172bea0cfdLuke Smith Y.log('Set attribute:' + name + ', aborted; Attribute is not configured', 'warn', 'attribute');
daa301d2a0f17b5c1b04d777de3acf969b9b63d2Luke Smith Y.log('Set attribute:' + name + ', aborted; Attribute is writeOnce', 'warn', 'attribute');
allowSet = false;
if (allowSet) {
if (path) {
allowSet = false;
if (allowSet) {
* @param {Object} opts Any additional event data to mix into the attribute change event's event facade.
var eventConfig = {
queuable:false,
silent:true
_defAttrChangeFn : function(e) {
Y.log('State not updated and stopImmediatePropagation called for attribute: ' + e.attrName + ' , value:' + e.newVal, 'warn', 'attribute');
* @param {String} subAttrName The sub-attribute name, if setting a sub-attribute property ("x.y.z").
var allowSet = true,
if (setter) {
Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal, 'warn', 'attribute');
allowSet = false;
Y.log('Attribute: ' + attrName + ', raw value: ' + newVal + ' modified by setter to:' + retVal, 'info', 'attribute');
if (allowSet) {
allowSet = false;
allowSet = false;
return allowSet;
* @param {Array | Boolean} attrs Optional. An array of attribute names, whose values are required. If omitted, all attribute values are
// Go through get, to honor cloning/normalization
* @param {Object} values Name/value hash of initial values to apply. Values defined in the configuration hash will be over-written by the initial values hash unless read-only.
if (cfgs) {
var attr,
var vals = {},
subvals = {},
path,
attr,
for (k in valueHash) {
v[v.length] = {
path,
return val;