YUI 3.x Home -

YUI Library Examples: Attribute: Basic Attribute Configuration

Attribute: Basic Attribute 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 augmenting Attribute directly, especially if you expect your class to be extended. Base does the work described in this example for you, in addition to making it easier for users to extend you class.

Construct o1, with default attribute values
Update the first instance, using set
Create the second instance, passing initial values to the constructor

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:

  1. <script type="text/javascript">
  2.  
  3. // Create our local YUI instance, to avoid
  4. // modifying the global YUI object
  5.  
  6. YUI({...}).use("attribute", "node", function(Y) {
  7.  
  8. // Example code is written inside this function,
  9. // which gets passed our own YUI instance, Y, loaded
  10. // with the modules we asked for - "attribute" and "node"
  11.  
  12. });
  13. </script>
<script type="text/javascript">
 
    // Create our local YUI instance, to avoid
    // modifying the global YUI object
 
    YUI({...}).use("attribute", "node", function(Y) {
 
        // Example code is written inside this function,
        // which gets passed our own YUI instance, Y, loaded
        // with the modules we asked for - "attribute" and "node"
 
    });
</script>

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:

  1. function MyClass(cfg) {
  2. ...
  3. }
  4.  
  5. Y.augment(MyClass, Y.Attribute);
function MyClass(cfg) {
    ...
}
 
Y.augment(MyClass, Y.Attribute);

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 gets passed to addAttrs to set up the attributes for each instance during construction.

The complete definition for MyClass is shown below:

  1. // Setup custom class which we want to add managed attribute support to
  2. function MyClass(cfg) {
  3.  
  4. // When constructed, setup the initial attributes for the
  5. // instance, by calling the addAttrs method.
  6. var attrs = {
  7. // Add 3 attributes, foo, bar and foobar
  8. "foo" : {
  9. value:5
  10. },
  11.  
  12. "bar" : {
  13. value:"Hello World!"
  14. },
  15.  
  16. "foobar" : {
  17. value:true
  18. }
  19. };
  20.  
  21. this.addAttrs(attrs, cfg);
  22. }
  23.  
  24. // Augment custom class with Attribute
  25. Y.augment(MyClass, Y.Attribute);
// Setup custom class which we want to add managed attribute support to
function MyClass(cfg) {
 
    // When constructed, setup the initial attributes for the 
    // instance, by calling the addAttrs method.
    var attrs = {
        // Add 3 attributes, foo, bar and foobar
        "foo" : {
            value:5
        },
 
        "bar" : {
            value:"Hello World!"
        },
 
        "foobar" : {
            value:true
        }
    };
 
    this.addAttrs(attrs, cfg);
}
 
// Augment custom class with Attribute 
Y.augment(MyClass, Y.Attribute);

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.

As mentioned previously, if you expect your class to be extended, Base provides a more convenient way for you to define the same attribute configuration statically for your class, so that it can be easily modified by extended classes. Base will take care of isolating the static configuration, so that it isn't modified across instances.

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:

We construct the first instance, o1, without setting any initial attribute values in the constructor, but use Attribute's set() method to set values after construction:

  1. // Create a new instance, but don't provide any initial attribute values.
  2. var o1 = new MyClass();
  3.  
  4. // Display current values
  5. displayValues(o1, "o1 with default values, set during construction",
  6. "#createo1 .example-out");
  7.  
  8. ...
  9.  
  10. // Update values, using the "set" method
  11. o1.set("foo", 10);
  12. o1.set("bar", "Hello New World!");
  13. o1.set("foobar", false);
  14.  
  15. displayValues(o1, "o1 values updated using set, after construction",
  16. "#updateo1 .example-out");
// Create a new instance, but don't provide any initial attribute values.
var o1 = new MyClass();
 
// Display current values
displayValues(o1, "o1 with default values, set during construction", 
                "#createo1 .example-out");
 
...
 
// Update values, using the "set" method
o1.set("foo", 10);
o1.set("bar", "Hello New World!");
o1.set("foobar", false);
 
displayValues(o1, "o1 values updated using set, after construction", 
                "#updateo1 .example-out");

For the second instance that, o2 we set the initial values of the attributes, using the constructor configuration argument:

  1. var o2 = new MyClass({
  2. foo: 7,
  3. bar: "Aloha World!",
  4. foobar: false
  5. });
var o2 = new MyClass({
    foo: 7,
    bar: "Aloha World!",
    foobar: false
});

The displayValues() method uses Attribute's get() method to retrieve the current values of the attributes, to display:

  1. function displayValues(o, title, node) {
  2. var str =
  3. '<div class="myclass"><div class="myclass-title">'
  4. + title +
  5. ':</div><ul class="myclass-attrs"><li>foo:'
  6. + o.get("foo")
  7. + '</li><li>bar:'
  8. + o.get("bar")
  9. + '</li><li>foobar:'
  10. + o.get("foobar")
  11. + '</li></ul></div>';
  12.  
  13. // Use the Y.one() method to get the first element which
  14. // matches the selector passed in, to output the string to...
  15. Y.one(node).set("innerHTML", str);
  16. }
function displayValues(o, title, node) {
    var str = 
        '<div class="myclass"><div class="myclass-title">' 
        + title + 
        ':</div><ul class="myclass-attrs"><li>foo:' 
        + o.get("foo") 
        + '</li><li>bar:'
        + o.get("bar")
        + '</li><li>foobar:'
        + o.get("foobar")
        + '</li></ul></div>';
 
    // Use the Y.one() method to get the first element which 
    // matches the selector passed in, to output the string to...       
    Y.one(node).set("innerHTML", str);
}

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings