5
<title>Example: Basic Attribute Configuration</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Basic Attribute Configuration</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><style type="text/css" scoped>
23
.example-out .myclass-attrs {
28
.example-out .myclass-title {
38
border:1px solid #000;
40
background-color:#004C6D;
43
padding:2px 2px 2px 5px;
48
<p>This example provides an introduction to the Attribute utility, showing how you can use it to add attribute support to your own custom classes.</p>
50
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 <a href="../base/index.html"><code>Base</code></a> class when you need managed attribute support,
51
instead of augmenting Attribute directly, especially if you expect your class to be extended. <a href="../base/index.html"><code>Base</code></a> does the work described in this example for you, in addition to making it easier for users to extend you class.
57
<button type="button" class="do">Create First Instance</button> Construct o1, with default attribute values
58
<div class="example-out"></div>
61
<button type="button" class="do">Update First Instance</button> Update the first instance, using set
62
<div class="example-out"></div>
65
<button type="button" class="do">Create Second Instance</button> Create the second instance, passing initial values to the constructor
66
<div class="example-out"></div>
69
<script type="text/javascript">
71
// Get a new instance of YUI and
72
// load it with the required set of modules
74
YUI().use("node", "attribute", function(Y) {
76
// Setup custom class which we want to
77
// add managed attribute support to
79
function MyClass(cfg) {
81
// When constructed, setup the initial attributes for the instance, by calling the addAttrs method.
83
// Add 3 attributes, foo, bar and foobar
97
this.addAttrs(attrs, cfg);
100
// Augment custom class with Attribute
101
Y.augment(MyClass, Y.Attribute);
103
function displayValues(o, title, node) {
105
'<div class="myclass"><div class="myclass-title">'
107
'</div><ul class="myclass-attrs"><li>foo: '
111
+ '</li><li>foobar: '
113
+ '</li></ul></div>';
115
Y.one(node).set("innerHTML", str);
118
Y.on("click", function() {
120
// Create a new instance, but don't provide any initial attribute values.
121
var o1 = new MyClass();
123
// Display current values
124
displayValues(o1, "o1 with default values, set during construction", "#createo1 .example-out");
126
Y.on("click", function() {
128
// Update values, using the "set" method
130
o1.set("bar", "Hello New World!");
131
o1.set("foobar", false);
133
displayValues(o1, "o1 values updated using set, after construction", "#updateo1 .example-out");
139
Y.on("click", function() {
141
var o2 = new MyClass({
147
displayValues(o2, "o2 values set during construction", "#createo2 .example-out");
155
<h2>Setting Up Your Own Class To Use Attribute</h2>
157
<p>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.</p>
159
<h3>Creating A YUI Instance</h3>
161
<p>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:</p>
163
<pre class="code prettyprint"><script type="text/javascript">
165
// Create our local YUI instance, to avoid
166
// modifying the global YUI object
168
YUI({...}).use("attribute", "node", function(Y) {
170
// Example code is written inside this function,
171
// which gets passed our own YUI instance, Y, loaded
172
// with the modules we asked for - "attribute" and "node"
175
</script></pre>
178
<p>The call to <code>YUI()</code> 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.</p>
180
<p>To load the modules, we invoke <code>use()</code> and pass it the list of modules we'd like populated on our new YUI instance - in this case, <code>attribute</code> and <code>node</code>.
182
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.
183
When the source files are done downloading, the callback function which we pass in as the 3rd argument to <code>use()</code>, is invoked. Our custom YUI instance, <code>Y</code>, is passed to the callback, populated with the classes which make up the requested modules.</p>
185
<p>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.</p>
187
<p>The configuration object passed to <code>YUI()</code> when creating the instance is used to specify how (<em>combined, separate, debug, min etc.</em>) we want the files downloaded, and from where. The API documentation for the <a href="http://yuilibrary.com/yui/docs/api/YUI.html">YUI object</a>, provides more information about the configuration options available.</p>
189
<h3>Defining Your Custom Class</h3>
191
<p>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 <code>MyClass</code>.
193
We then augment <code>MyClass</code> with <code>Y.Attribute</code>, so that it receives all of <code>Attribute's</code> methods:</p>
195
<pre class="code prettyprint">function MyClass(cfg) {
199
Y.augment(MyClass, Y.Attribute);</pre>
202
<h3>Adding Attributes</h3>
204
<p>We can now set up any attributes we need for <code>MyClass</code> using the <code>addAttrs</code> method. For the basic example we add 3 attributes - <code>foo</code>,<code>bar</code>, and <code>foobar</code>, and provide an initial <code>value</code> for each.
206
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 <code>readOnly</code> or <code>writeOnce</code>, and define <code>getter</code>, <code>setter</code> and <code>validator</code> methods for the attribute.</p>
208
<p>In this example, the default set of attributes which <code>MyClass</code> will support gets passed to <code>addAttrs</code> to set up the attributes for each instance during construction.</p>
210
The complete definition for <code>MyClass</code> is shown below:</p>
212
<pre class="code prettyprint">// Setup custom class which we want to add managed attribute support to
213
function MyClass(cfg) {
215
// When constructed, setup the initial attributes for the
216
// instance, by calling the addAttrs method.
218
// Add 3 attributes, foo, bar and foobar
224
value:"Hello World!"
227
"foobar" : {
232
this.addAttrs(attrs, cfg);
235
// Augment custom class with Attribute
236
Y.augment(MyClass, Y.Attribute);</pre>
239
<p>The <code>addAttrs</code> 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 to set the value of attributes as part of object construction, as shown by the use of the <code>cfg</code> argument above.</p>
242
As mentioned previously, if you expect your class to be extended, <a href="../base/index.html">Base</a> 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.
245
<h3>Using Attributes</h3>
247
<p>Now that we have <code>MyClass</code> defined with a set of attributes it supports, users can get and set attribute values on instances of <code>MyClass</code>:</p>
249
<p>We construct the first instance, <code>o1</code>, without setting any initial attribute values in the constructor, but use Attribute's <code>set()</code> method to set values after construction:</p>
251
<pre class="code prettyprint">// Create a new instance, but don't provide any initial attribute values.
252
var o1 = new MyClass();
254
// Display current values
255
displayValues(o1, "o1 with default values, set during construction",
256
"#createo1 .example-out");
260
// Update values, using the "set" method
261
o1.set("foo", 10);
262
o1.set("bar", "Hello New World!");
263
o1.set("foobar", false);
265
displayValues(o1, "o1 values updated using set, after construction",
266
"#updateo1 .example-out");</pre>
269
<p>For the second instance that, <code>o2</code> we set the initial values of the attributes, using the constructor configuration argument:</p>
271
<pre class="code prettyprint">var o2 = new MyClass({
273
bar: "Aloha World!",
278
<p>The <code>displayValues()</code> method uses Attribute's <code>get()</code> method to retrieve the current values of the attributes, to display:</p>
280
<pre class="code prettyprint">function displayValues(o, title, node) {
282
'<div class="myclass"><div class="myclass-title">'
284
':</div><ul class="myclass-attrs"><li>foo:'
285
+ o.get("foo")
286
+ '</li><li>bar:'
287
+ o.get("bar")
288
+ '</li><li>foobar:'
289
+ o.get("foobar")
290
+ '</li></ul></div>';
292
// Use the Y.one() method to get the first element which
293
// matches the selector passed in, to output the string to...
294
Y.one(node).set("innerHTML", str);
298
<h2>Complete Example Source</h2>
300
<pre class="code prettyprint"><div id="createo1">
301
<button type="button" class="do">Create First Instance</button> Construct o1, with default attribute values
302
<div class="example-out"></div>
304
<div id="updateo1">
305
<button type="button" class="do">Update First Instance</button> Update the first instance, using set
306
<div class="example-out"></div>
308
<div id="createo2">
309
<button type="button" class="do">Create Second Instance</button> Create the second instance, passing initial values to the constructor
310
<div class="example-out"></div>
313
<script type="text/javascript">
315
// Get a new instance of YUI and
316
// load it with the required set of modules
318
YUI().use("node", "attribute", function(Y) {
320
// Setup custom class which we want to
321
// add managed attribute support to
323
function MyClass(cfg) {
325
// When constructed, setup the initial attributes for the instance, by calling the addAttrs method.
327
// Add 3 attributes, foo, bar and foobar
333
value:"Hello World!"
336
"foobar" : {
341
this.addAttrs(attrs, cfg);
344
// Augment custom class with Attribute
345
Y.augment(MyClass, Y.Attribute);
347
function displayValues(o, title, node) {
349
'<div class="myclass"><div class="myclass-title">'
351
'</div><ul class="myclass-attrs"><li>foo: '
352
+ o.get("foo")
353
+ '</li><li>bar: '
354
+ o.get("bar")
355
+ '</li><li>foobar: '
356
+ o.get("foobar")
357
+ '</li></ul></div>';
359
Y.one(node).set("innerHTML", str);
362
Y.on("click", function() {
364
// Create a new instance, but don't provide any initial attribute values.
365
var o1 = new MyClass();
367
// Display current values
368
displayValues(o1, "o1 with default values, set during construction", "#createo1 .example-out");
370
Y.on("click", function() {
372
// Update values, using the "set" method
373
o1.set("foo", 10);
374
o1.set("bar", "Hello New World!");
375
o1.set("foobar", false);
377
displayValues(o1, "o1 values updated using set, after construction", "#updateo1 .example-out");
379
}, "#updateo1 .do");
381
}, "#createo1 .do");
383
Y.on("click", function() {
385
var o2 = new MyClass({
387
bar: "Aloha World!",
391
displayValues(o2, "o2 values set during construction", "#createo2 .example-out");
393
}, "#createo2 .do");
395
</script></pre>
401
<div class="yui3-u-1-4">
402
<div class="sidebar">
406
<div class="sidebox">
408
<h2 class="no-toc">Examples</h2>
412
<ul class="examples">
415
<li data-description="Use the Attribute API to define, set and get attribute values.">
416
<a href="attribute-basic.html">Basic Attribute Configuration</a>
421
<li data-description="Configure attributes to be readOnly or writeOnce.">
422
<a href="attribute-rw.html">Read-Only and Write-Once Attributes</a>
427
<li data-description="How to listen for changes in attribute values.">
428
<a href="attribute-event.html">Attribute Change Events</a>
433
<li data-description="Create a basic SpeedDater class, with Attribute support.">
434
<a href="attribute-basic-speeddate.html">Attribute Based Speed Dating</a>
439
<li data-description="Refactors the basic Speed Dating example, to use attribute change events to update rendered elements, and have two instances react to another.">
440
<a href="attribute-event-speeddate.html">Attribute Event Based Speed Dating</a>
445
<li data-description="Add custom methods to get and set attribute values and provide validation support.">
446
<a href="attribute-getset.html">Attribute Getters, Setters and Validators</a>
461
<script src="../assets/vendor/prettify/prettify-min.js"></script>
462
<script>prettyPrint();</script>