YUI 3.x Home -

YUI Library Examples: Attribute: Custom Getters, Setters and Validators

Note: This is YUI 3.x. Looking for YUI 2.x?

Attribute: Custom Getters, Setters and Validators

The Basic Configuration example shows how we can add attributes to a host class, and set up default values for them using the attribute configuration object. This example explores how you can configure set, get and validator functions for individual attributes, which can be used to modify or normalize attribute values during get and set invocations, and prevent invalid values from being stored.

Enter new values and click the "Set" buttons
  • Try entering both valid and invalid values for x, y; or values which would place the box outside it's parent.
  • Try entering rgb (e.g. rgb(255,0,0)), hex (e.g. #ff0000) or keyword (e.g. red) color values.

Get, Set And Validator Functions

Attribute lets you configure get and set functions for each attribute. These functions are invoked when the user calls Attribute's get and set methods, and provide a way to modify the value returned or the value stored respectively.

You can also define a validator function for each attribute, which is used to validate the final value before it gets stored.

Creating The Box Class - The X, Y And XY Attributes

In this example, we'll set up a custom Box class representing a positionable element, with x, y and xy attributes.

Only the xy attribute will actually store the page co-ordinate position of the box. The x and y attributes provide the user a convenient way to set only one of the co-ordinates. However we don't want to store the actual values in the x and y attributes, to avoid having to constantly sync state between all three. The get and set functions provide us with an easy way to achieve this. We'll define get and set functions for both the x and y attributes, which simply pass through to the xy attribute to retrieve and store values:

The validator function for xy ensures that only valid values finally end up being stored.

The xy attribute also has a set function configured, which makes sure that the box is always constrained to it's parent element. The constrain method which it delegates to, takes the xy value the user is trying to set and returns a modified, constrained value if required. The value which is returned by the set handler is the value which is ultimately stored for the xy attribute:

The set, get and validator functions are invoked with the host object as the context, so that they can refer to the host object using "this", as we see in the set function for xy.

The Color Attribute - Normalizing Stored Values Through Get

The Box class also has a color attribute which also has a get handler and a validator defined:

The role of the get handler in this case is to normalize the actual stored value of the color attribute, so that users always receive the hex value, regardless of the actual value stored, which maybe a color keyword (e.g. "red"), an rgb value (e.g.rbg(255,0,0)), or a hex value (#ff0000). The validator ensures the the stored value is one of these three formats.

Syncing Changes Using Attribute Change Events

Another interesting aspect of this example, is it's use of attribute change events to listen for changes to the attribute values. Box's _bind method configures a set of attribute change event listeners which monitor changes to the xy, color and parent attributes and update the rendered DOM for the Box in response:

Since only xy stores the final co-ordinates, we don't need to monitor the x and y attributes individually for changes.

DOM Event Listeners

Although not an integral part of the example, it's worth highlighting the code which is used to setup the DOM event listeners for the form elements used by the example:

The above code uses the new 3.x Event facade which normalizes cross-browser access to DOM event properties, such as target. We use target to delegate event handling for click events which bubble up to the attrs element. Note the use of selector syntax when we specify the element(s) to which we want to attach DOM listeners (e.g. #attrs) and the use of the Node facade when dealing with references to HTML elements (e.g. xTxt, yTxt, colorTxt.

Copyright © 2008 Yahoo! Inc. All rights reserved.

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