3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('attribute-extras', function(Y) {
10
* The attribute module provides an augmentable Attribute implementation, which
11
* adds configurable attributes and attribute change events to the class being
12
* augmented. It also provides a State class, which is used internally by Attribute,
13
* but can also be used independently to provide a name/property/value data structure to
20
* The attribute-extras submodule provides less commonly used attribute methods, and can
21
* be augmented/mixed into an implemention which used attribute-core.
24
* @submodule attribute-extras
26
var BROADCAST = "broadcast",
27
PUBLISHED = "published",
28
INIT_VALUE = "initValue",
38
* A augmentable implementation for AttributeCore, providing less frequently used
39
* methods for Attribute management such as modifyAttrs(), removeAttr and reset()
41
* @class AttributeExtras
43
function AttributeExtras() {}
45
AttributeExtras.prototype = {
48
* Updates the configuration of an attribute which has already been added.
50
* The properties which can be modified through this interface are limited
51
* to the following subset of attributes, which can be safely modified
52
* after a value has already been set on the attribute: readOnly, writeOnce,
53
* broadcast and getter.
56
* @param {String} name The name of the attribute whose configuration is to be updated.
57
* @param {Object} config An object with configuration property/value pairs, specifying the configuration properties to modify.
59
modifyAttr: function(name, config) {
60
var host = this, // help compression
63
if (host.attrAdded(name)) {
65
if (host._isLazyAttr(name)) {
66
host._addLazyAttr(name);
70
for (prop in config) {
71
if (MODIFIABLE[prop] && config.hasOwnProperty(prop)) {
72
state.add(name, prop, config[prop]);
74
// If we reconfigured broadcast, need to republish
75
if (prop === BROADCAST) {
76
state.remove(name, PUBLISHED);
85
* Removes an attribute from the host object
88
* @param {String} name The name of the attribute to be removed.
90
removeAttr: function(name) {
91
this._state.removeAll(name);
95
* Resets the attribute (or all attributes) to its initial value, as long as
96
* the attribute is not readOnly, or writeOnce.
99
* @param {String} name Optional. The name of the attribute to reset. If omitted, all attributes are reset.
100
* @return {Object} A reference to the host object.
103
reset : function(name) {
104
var host = this; // help compression
107
if (host._isLazyAttr(name)) {
108
host._addLazyAttr(name);
110
host.set(name, host._state.get(name, INIT_VALUE));
112
Y.each(host._state.data, function(v, n) {
120
* Returns an object with the configuration properties (and value)
121
* for the given attribute. If attrName is not provided, returns the
122
* configuration properties for all attributes.
124
* @method _getAttrCfg
126
* @param {String} name Optional. The attribute name. If not provided, the method will return the configuration for all attributes.
127
* @return {Object} The configuration properties for the given attribute, or all attributes.
129
_getAttrCfg : function(name) {
134
o = state.getAll(name) || {};
137
Y.each(state.data, function(v, n) {
138
o[n] = state.getAll(n);
146
Y.AttributeExtras = AttributeExtras;