Attribute: Basic Configuration
+ ++
This example provides an introduction to the Attribute utility, showing how you can use it to add attribute support to your own custom classes.
+ +It is geared towards users who want to create their own classes from scratch and add Attribute support.
+In most cases you should consider extending the Base class when you need managed attribute support,
+instead of creating your own class from scratch.
+
+Base does the work described in this example for you, in addition to making it easier for users to extend you class.
+
Setting Up Your Own Class To Use Attribute
+ +In this example, we'll show how you can use the Attribute utility to add managed attributes to your own object classes. Later examples will show how you can configure more advanced attribute properties, and work with attribute change events.
+ +Creating A YUI Instance
+ +Before we get into attribute, a quick note on how we set up the instance of YUI we'll use for the examples. For all of the attribute examples, we'll setup our own instance of the YUI object and download the files we require on demand using the code pattern shown below:
+ + + +The call to YUI() will create and return a new instance of the global YUI object for us to use. However this instance does not yet have all the modules we need for the examples.
To load the modules, we invoke use() and pass it the list of modules we'd like populated on our new YUI instance - in this case, attribute and node.
+
+The YUI instance will pull down the source files for modules if they don't already exist on the page, plus any or their dependencies.
+When the source files are done downloading, the callback function which we pass in as the 3rd argument to use(), is invoked. Our custom YUI instance, Y, is passed to the callback, populated with the classes which make up the requested modules.
This callback function is where we'll write all our example code. By working inside the callback function, we don't pollute the global namespace and we're also able to download the files we need on demand, rather than have them be on the page up front.
+ +The configuration object passed to YUI() when creating the instance is used to specify how (combined, separate, debug, min etc.) we want the files downloaded, and from where. The API documentation for the YUI object, provides more information about the configuration options available.
Defining Your Custom Class
+ +The first step in the example is to create the constructor function for our new class, to which we want to add attribute support. In our example, this class is called MyClass.
+
+We then augment MyClass with Y.Attribute, so that it receives all of Attribute's methods:
Adding Attributes
+ +We can now set up any attributes we need for MyClass using the addAttrs method. For the basic example we add 3 attributes - foo,bar, and foobar, and provide an initial value for each.
+
+The same object literal we use to provide the initial value for the attribute will also be used in the other examples to configure attribute properties such as readOnly or writeOnce, and define getter, setter and validator methods for the attribute.
In this example, the default set of attributes which MyClass will support is defined as a static property, ATTRIBUTES, on the constructor function. This gets passed to addAttrs to set up the attributes for each instance during construction.
+
+This is not required by Attribute, but allows the default attribute configuration to be easily customized by developers who may want to modify/extend our class. However, as a result of ATTRIBUTES being statically defined, we need to use Y.merge to create a shallow copy, before passing it to addAttrs, so that the static configuration is not modified by the instance.
+
+The complete definition for MyClass is shown below:
The addAttrs method, in addition to the default attribute configuration, also accepts an object literal (associative array) of name/value pairs which can be used to over-ride the default initial values of the attributes. This is useful for classes which wish to allow the user the set the value of attributes as part of object construction, as shown by the use of the cfg argument above.
Using Attributes
+ +Now that we have MyClass defined with a set of attributes it supports, users can get and set attribute values on instances of MyClass:
For the second instance that we create in the example we set the values of the attributes, using the constructor configuration argument:
+ + +