The "Basic Attribute Configuration" example shows how you 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 setter
, getter
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.
- Try entering valid and invalid values for x, y; or values which attempt to position the box outside it's parent (parent box co-ordinates are displayed next to the text box).
- Try entering rgb, hex or keyword color values [
rgb(255,0,0)
,#ff0000
,red
].
Getter, Setter And Validator Functions
Attribute lets you configure getter
and setter
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.
All these functions receive the value and name of the attribute being set or retrieved, as shown in the example code below. The name is not used in this example, but is provided to support use cases where you may wish to share the same function between different attributes.
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 synchronize all three.
The getter
and setter
functions provide us with an easy way to achieve this. We'll define getter
and setter
functions for both the x
and y
attributes, which simply pass through to the xy
attribute to store and retrieve values:
// Setup a custom class with attribute support function Box(cfg) { ... // Attribute configuration var attrs = { "parent" : { value: null }, "x" : { setter: function(val, name) { // Pass through x value to xy this.set("xy", [val, this.get("y")]); }, getter: function(val, name) { // Get x value from xy return this.get("xy")[0]; } }, "y" : { setter: function(val, name) { // Pass through y value to xy this.set("xy", [this.get("x"), val]); }, getter: function() { // Get y value from xy return this.get("xy")[1]; } }, "xy" : { // Actual stored xy co-ordinates value: [0, 0], setter: function(val, name) { // Constrain XY value to the parent element. // Returns the constrained xy value, which will // be the final value stored. return this.constrain(val); }, validator: function(val, name) { // Ensure we only store a valid data value return (Y.Lang.isArray(val) && val.length == 2 && Y.Lang.isNumber(val[0]) && Y.Lang.isNumber(val[1])); } }, ... this.addAttrs(attrs, cfg); ... }
The validator
function for xy
ensures that only valid values finally end up being stored.
The xy
attribute also has a setter
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 constrained value if the x or y values fall outside the parent box. The value which is returned by the setter
is the value which is ultimately stored for the xy
attribute:
// Get min, max unconstrained values for X. Box.prototype.getXConstraints = function() { var parentRegion = this.get("parent").get("region"), nodeRegion = this._node.get("region"), nodeWidth = nodeRegion.right-nodeRegion.left; // Ceil/Floor to account for browsers which have sub-pixel values. return [Math.ceil(parentRegion.left + Box.BUFFER), Math.floor(parentRegion.right - nodeWidth - Box.BUFFER)]; }; // Get min, max unconstrained values for Y. Box.prototype.getYConstraints = function() { var parentRegion = this.get("parent").get("region"), nodeRegion = this._node.get("region"), nodeHeight = nodeRegion.bottom-nodeRegion.top; // Ceil/Floor to account for browsers which have sub-pixel values. return [Math.ceil(parentRegion.top + Box.BUFFER), Math.floor(parentRegion.bottom - nodeHeight - Box.BUFFER)]; }; // Constrains given x,y values Box.prototype.constrain = function(val) { // If the X value places the box outside it's parent, // modify it's value to place the box inside it's parent. var xConstraints = this.getXConstraints(); if (val[0] < xConstraints[0]) { val[0] = xConstraints[0]; } else { if (val[0] > xConstraints[1]) { val[0] = xConstraints[1]; } } // If the Y value places the box outside it's parent, // modify it's value to place the box inside it's parent. var yConstraints = this.getYConstraints(); if (val[1] < yConstraints[0]) { val[1] = yConstraints[0]; } else { if (val[1] > yConstraints[1]) { val[1] = yConstraints[1]; } } return val; };
The setter
, getter
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 setter
function for xy
.
The Color Attribute - Normalizing Stored Values Through Get
The Box
class also has a color
attribute which also has a getter
and validator
functions defined:
... "color" : { value: "olive", getter: function(val, name) { if (val) { return Y.Color.toHex(val); } else { return null; } }, validator: function(val, name) { return (Y.Color.re_RGB.test(val) || Y.Color.re_hex.test(val) || Y.Color.KEYWORDS[val]); } } ...
The role of the getter
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:
// Bind listeners for attribute change events Box.prototype._bind = function() { // Reflect any changes in xy, to the rendered Node this.after("xyChange", this._syncXY); // Reflect any changes in color, to the rendered Node // and output the color value received from get this.after("colorChange", this._syncColor); // Append the rendered node to the parent provided this.after("parentChange", this._syncParent); };
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 And Delegation
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:
// Set references to form controls var xTxt = Y.one("#x"); var yTxt = Y.one("#y"); var colorTxt = Y.one("#color"); // Use event delegation for the action button clicks, and form submissions Y.delegate("click", function(e) { // Get Node target from the event object // We already know it's a button which has an action because // of our selector (button.action), so all we need to do is // route it based on the id. var id = e.currentTarget.get("id"); switch (id) { case "setXY": box.set("xy", [parseInt(xTxt.get("value")), parseInt(yTxt.get("value"))]); break; case "setAll": box.set("xy", [parseInt(xTxt.get("value")), parseInt(yTxt.get("value"))]); box.set("color", Y.Lang.trim(colorTxt.get("value"))); break; case "getAll": getAll(); break; default: break; } }, "#attrs", "button.action");
Rather than attach individual listeners to each button, the above code uses YUI 3's delegate
support, to listen for click
from buttons, with an action
class which bubble up to the attrs
element.
The delegate listener uses the Event Facade which normalizes cross-browser access to DOM event properties, such as currentTarget
, to route to the appropriate button handler. Note the use of selector syntax when we specify the elements for the listener (e.g. #attrs
, button.actions
) and the use of the Node facade when dealing with references to HTML elements (e.g. xTxt, yTxt, colorTxt
).
Complete Example Source
<div id="attrs" class="attrs"> <div class="header">Enter new values and click the "Set" buttons</div> <div class="body"> <ul class="hints"> <li>Try entering valid and invalid values for x, y; or values which attempt to position the box outside it's parent (parent box co-ordinates are displayed next to the text box).</li> <li>Try entering rgb, hex or keyword color values [ <code>rgb(255,0,0)</code>, <code>#ff0000</code>, <code>red</code> ].</li> </ul> <div class="fields"> <p> <form action="#" id="setX" class="action"> <label for="x">x:</label> <input type="text" name="x" id="x" /> <button type="submit">Set</button> <span id="xhint" class="hint"></span> </form> </p> <p> <form action="#" id="setY" class="action"> <label for="y">y:</label> <input type="text" name="y" id="y" /> <button type="submit">Set</button> <span id="yhint" class="hint"></span> </form> </p> <p> <form action="#" id="setColor" class="action"> <label for="color">color:</label> <input type="text" name="color" id="color" /> <button type="submit">Set</button> </form> </p> </div> </div> <div class="footer"> <button type="button" class="action" id="setXY">Set XY</button> <button type="button" class="action" id="setAll">Set All</button> <button type="button" class="action" id="getAll">Get All</button> </div> </div> <div id="boxParent"></div> <script type="text/javascript"> // Get a new YUI instance YUI().use("node", "attribute", function(Y) { var boxParent = Y.one("#boxParent"); // Setup a custom class with attribute support function Box(cfg) { this._createNode(cfg); // Attribute configuration var attrs = { "parent" : { value: null }, "x" : { setter: function(val, name) { // Pass through x value to xy this.set("xy", [val, this.get("y")]); }, getter: function(val, name) { // Get x value from xy return this.get("xy")[0]; } }, "y" : { setter: function(val, name) { // Pass through y value to xy this.set("xy", [this.get("x"), val]); }, getter: function() { // Get y value from xy return this.get("xy")[1]; } }, "xy" : { // Actual stored xy co-ordinates value: [0, 0], setter: function(val, name) { // Constrain XY value to the parent element. // Returns the constrained xy value, which will // be the final value stored. return this.constrain(val); }, validator: function(val, name) { // Ensure we only store a valid data value return (Y.Lang.isArray(val) && val.length == 2 && Y.Lang.isNumber(val[0]) && Y.Lang.isNumber(val[1])); } }, "color" : { value: "olive", getter: function(val, name) { // Always normalize the returned value to // a hex color value, even if the stored // value is a keyword, or an rgb value. if (val) { return Y.Color.toHex(val); } else { return null; } }, validator: function(val, name) { // Ensure we only store rgb, hex or keyword values. return (Y.Color.re_RGB.test(val) || Y.Color.re_hex.test(val) || Y.Color.KEYWORDS[val]); } } }; this.addAttrs(attrs, cfg); this._sync(); this._bind(); } Box.BUFFER = 5; // Create the box node Box.prototype._createNode = function() { this._node = Y.Node.create('<div class="yui3-box"><p>Positioned Box</p><p class="coord"></p><p class="color">None</p></div>'); }; // Update rendered state to match the attribute state Box.prototype._sync = function() { this._syncParent(); this._syncXY(); this._syncColor(); }; Box.prototype._syncParent = function() { this.get("parent").appendChild(this._node); }; Box.prototype._syncXY = function() { this._node.setXY(this.get("xy")); this._node.one("p.coord").set("innerHTML", "[" + this.get("x") + "," + this.get("y") + "]"); }; Box.prototype._syncColor = function() { this._node.setStyle("backgroundColor", this.get("color")); this._node.one("p.color").set("innerHTML", this.get("color")); }; // Bind listeners for attribute change events Box.prototype._bind = function() { // Reflect any changes in xy, to the rendered Node this.after("xyChange", this._syncXY); // Reflect any changes in color, to the rendered Node // and output the color value received from get this.after("colorChange", this._syncColor); // Append the rendered node to the parent provided this.after("parentChange", this._syncParent); }; // Get min, max unconstrained values for X. Box.prototype.getXConstraints = function() { var parentRegion = this.get("parent").get("region"), nodeRegion = this._node.get("region"), nodeWidth = nodeRegion.right-nodeRegion.left; // Ceil/Floor to account for browsers which have sub-pixel values. return [Math.ceil(parentRegion.left + Box.BUFFER), Math.floor(parentRegion.right - nodeWidth - Box.BUFFER)]; }; // Get min, max unconstrained values for Y. Box.prototype.getYConstraints = function() { var parentRegion = this.get("parent").get("region"), nodeRegion = this._node.get("region"), nodeHeight = nodeRegion.bottom-nodeRegion.top; // Ceil/Floor to account for browsers which have sub-pixel values. return [Math.ceil(parentRegion.top + Box.BUFFER), Math.floor(parentRegion.bottom - nodeHeight - Box.BUFFER)]; }; // Constrain the x,y value provided Box.prototype.constrain = function(val) { // If the X value places the box outside it's parent, // modify it's value to place the box inside it's parent. var xConstraints = this.getXConstraints(); if (val[0] < xConstraints[0]) { val[0] = xConstraints[0]; } else { if (val[0] > xConstraints[1]) { val[0] = xConstraints[1]; } } // If the Y value places the box outside it's parent, // modify it's value to place the box inside it's parent. var yConstraints = this.getYConstraints(); if (val[1] < yConstraints[0]) { val[1] = yConstraints[0]; } else { if (val[1] > yConstraints[1]) { val[1] = yConstraints[1]; } } return val; }; Y.augment(Box, Y.Attribute); // ------ // Create a new instance of Box var box = new Box({ parent : boxParent }); // Set references to form controls var xTxt = Y.one("#x"); var yTxt = Y.one("#y"); var colorTxt = Y.one("#color"); var xHint = Y.one("#xhint"); var yHint = Y.one("#yhint"); function getAll() { xTxt.set("value", box.get("x")); yTxt.set("value", box.get("y")); colorTxt.set("value", box.get("color")); } // Use event delegation for the action button clicks, and form submissions Y.delegate("click", function(e) { // Get Node target from the event object // We already know it's a button which has an action because // of our selector (button.action), so all we need to do is // route it based on the id. var id = e.currentTarget.get("id"); switch (id) { case "setXY": box.set("xy", [parseInt(xTxt.get("value"), 10), parseInt(yTxt.get("value"), 10)]); break; case "setAll": box.set("xy", [parseInt(xTxt.get("value"), 10), parseInt(yTxt.get("value"), 10)]); box.set("color", Y.Lang.trim(colorTxt.get("value"))); break; case "getAll": getAll(); break; default: break; } }, "#attrs", "button.action"); Y.all("#attrs form.action").on("submit", function(e) { e.preventDefault(); // Get Node target from the event object // We already know it's a button which has an action because // of our selector (button.action), so all we need to do is // route it based on the id. var id = e.currentTarget.get("id"); switch (id) { case "setX": box.set("x", parseInt(xTxt.get("value"), 10)); break; case "setY": box.set("y", parseInt(yTxt.get("value"), 10)); break; case "setColor": box.set("color", Y.Lang.trim(colorTxt.get("value"))); break; default: break; } }); // Bind listeners to provide min, max unconstrained value hints for x, y // (focus/blur doesn't bubble, so bind individual listeners) Y.on("focus", function() { var minmax = box.getXConstraints(); xHint.set("innerHTML", "Valid values: " + minmax[0] + " to " + minmax[1]); }, xTxt); Y.on("focus", function() { var minmax = box.getYConstraints(); yHint.set("innerHTML", "Valid values: " + minmax[0] + " to " + minmax[1]); }, yTxt); Y.on("blur", function() { xHint.set("innerHTML", ""); }, xTxt); Y.on("blur", function() { yHint.set("innerHTML", ""); }, yTxt); getAll(); }); </script>