~smagoun/whoopsie/whoopsie-lp1017637

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/docs/attribute/attribute-basic.html

  • Committer: Evan Dandrea
  • Date: 2012-05-09 05:53:45 UTC
  • Revision ID: evan.dandrea@canonical.com-20120509055345-z2j41tmcbf4as5uf
The backend now lives in lp:daisy and the website (errors.ubuntu.com) now lives in lp:errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html>
2
 
<html lang="en">
3
 
<head>
4
 
    <meta charset="utf-8">
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>
11
 
</head>
12
 
<body>
13
 
 
14
 
<div id="doc">
15
 
    <h1>Example: Basic Attribute Configuration</h1>
16
 
 
17
 
    
18
 
 
19
 
    <div class="yui3-g">
20
 
        <div class="yui3-u-3-4">
21
 
            <div id="main">
22
 
                <div class="content"><style type="text/css" scoped>
23
 
    .example-out .myclass-attrs {
24
 
        font-family:courier;
25
 
        margin-top:2px;
26
 
    }
27
 
 
28
 
    .example-out .myclass-title {
29
 
        font-weight:bold;
30
 
        font-family:arial;
31
 
        color:#8dd5e7;
32
 
        margin-top:5px;
33
 
        margin-bottom:3px;
34
 
    }
35
 
 
36
 
    .example-out {
37
 
        overflow:auto;
38
 
        border:1px solid #000;
39
 
        color:#ffffff;
40
 
        background-color:#004C6D;
41
 
        margin:5px;
42
 
        height:8em;
43
 
        padding:2px 2px 2px 5px;
44
 
    }
45
 
</style>
46
 
 
47
 
<div class="intro">
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>
49
 
    <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.
52
 
    </p>    
53
 
</div>
54
 
 
55
 
<div class="example">
56
 
    <div id="createo1">
57
 
    <button type="button" class="do">Create First Instance</button> Construct o1, with default attribute values
58
 
    <div class="example-out"></div>
59
 
</div>
60
 
<div id="updateo1">
61
 
    <button type="button" class="do">Update First Instance</button> Update the first instance, using set
62
 
    <div class="example-out"></div>
63
 
</div>
64
 
<div id="createo2">
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>
67
 
</div>
68
 
 
69
 
<script type="text/javascript">
70
 
 
71
 
// Get a new instance of YUI and 
72
 
// load it with the required set of modules
73
 
 
74
 
YUI().use("node", "attribute", function(Y) {
75
 
 
76
 
    // Setup custom class which we want to 
77
 
    // add managed attribute support to
78
 
 
79
 
    function MyClass(cfg) {
80
 
 
81
 
        // When constructed, setup the initial attributes for the instance, by calling the addAttrs method.
82
 
        var attrs = {
83
 
            // Add 3 attributes, foo, bar and foobar
84
 
            "foo" : {
85
 
                value:5
86
 
            },
87
 
     
88
 
            "bar" : {
89
 
                value:"Hello World!"
90
 
            },
91
 
    
92
 
            "foobar" : {
93
 
                value:true
94
 
            }
95
 
        };
96
 
 
97
 
        this.addAttrs(attrs, cfg);
98
 
    }
99
 
 
100
 
    // Augment custom class with Attribute
101
 
    Y.augment(MyClass, Y.Attribute);
102
 
 
103
 
    function displayValues(o, title, node) {
104
 
        var str = 
105
 
            '<div class="myclass"><div class="myclass-title">' 
106
 
            + title + 
107
 
            '</div><ul class="myclass-attrs"><li>foo: ' 
108
 
            + o.get("foo") 
109
 
            + '</li><li>bar: '
110
 
            + o.get("bar")
111
 
            + '</li><li>foobar: '
112
 
            + o.get("foobar")
113
 
            + '</li></ul></div>';
114
 
 
115
 
        Y.one(node).set("innerHTML", str);
116
 
    }
117
 
 
118
 
    Y.on("click", function() {
119
 
 
120
 
        // Create a new instance, but don't provide any initial attribute values.
121
 
        var o1 = new MyClass();
122
 
 
123
 
        // Display current values
124
 
        displayValues(o1, "o1 with default values, set during construction", "#createo1 .example-out");
125
 
 
126
 
        Y.on("click", function() {
127
 
 
128
 
            // Update values, using the "set" method
129
 
            o1.set("foo", 10);
130
 
            o1.set("bar", "Hello New World!");
131
 
            o1.set("foobar", false);
132
 
 
133
 
            displayValues(o1, "o1 values updated using set, after construction", "#updateo1 .example-out");
134
 
 
135
 
        }, "#updateo1 .do");
136
 
 
137
 
    }, "#createo1 .do");
138
 
 
139
 
    Y.on("click", function() {
140
 
 
141
 
        var o2 = new MyClass({
142
 
            foo: 7,
143
 
            bar: "Aloha World!",
144
 
            foobar: false
145
 
        });
146
 
 
147
 
        displayValues(o2, "o2 values set during construction", "#createo2 .example-out");
148
 
 
149
 
    }, "#createo2 .do");
150
 
});
151
 
</script>
152
 
 
153
 
</div>
154
 
 
155
 
<h2>Setting Up Your Own Class To Use Attribute</h2>
156
 
 
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>
158
 
 
159
 
<h3>Creating A YUI Instance</h3>
160
 
 
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>
162
 
 
163
 
<pre class="code prettyprint">&lt;script type=&quot;text&#x2F;javascript&quot;&gt;
164
 
 
165
 
    &#x2F;&#x2F; Create our local YUI instance, to avoid
166
 
    &#x2F;&#x2F; modifying the global YUI object
167
 
 
168
 
    YUI({...}).use(&quot;attribute&quot;, &quot;node&quot;, function(Y) {
169
 
 
170
 
        &#x2F;&#x2F; Example code is written inside this function,
171
 
        &#x2F;&#x2F; which gets passed our own YUI instance, Y, loaded
172
 
        &#x2F;&#x2F; with the modules we asked for - &quot;attribute&quot; and &quot;node&quot;
173
 
 
174
 
    });
175
 
&lt;&#x2F;script&gt;</pre>
176
 
 
177
 
 
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>
179
 
 
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>.
181
 
 
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>
184
 
 
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>
186
 
 
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>
188
 
 
189
 
<h3>Defining Your Custom Class</h3>
190
 
 
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>.
192
 
 
193
 
We then augment <code>MyClass</code> with <code>Y.Attribute</code>, so that it receives all of <code>Attribute&#x27;s</code> methods:</p>
194
 
 
195
 
<pre class="code prettyprint">function MyClass(cfg) {
196
 
    ...
197
 
}
198
 
 
199
 
Y.augment(MyClass, Y.Attribute);</pre>
200
 
 
201
 
 
202
 
<h3>Adding Attributes</h3>
203
 
 
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. 
205
 
 
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>
207
 
 
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>
209
 
 
210
 
The complete definition for <code>MyClass</code> is shown below:</p>
211
 
 
212
 
<pre class="code prettyprint">&#x2F;&#x2F; Setup custom class which we want to add managed attribute support to
213
 
function MyClass(cfg) {
214
 
 
215
 
    &#x2F;&#x2F; When constructed, setup the initial attributes for the 
216
 
    &#x2F;&#x2F; instance, by calling the addAttrs method.
217
 
    var attrs = {
218
 
        &#x2F;&#x2F; Add 3 attributes, foo, bar and foobar
219
 
        &quot;foo&quot; : {
220
 
            value:5
221
 
        },
222
 
 
223
 
        &quot;bar&quot; : {
224
 
            value:&quot;Hello World!&quot;
225
 
        },
226
 
 
227
 
        &quot;foobar&quot; : {
228
 
            value:true
229
 
        }
230
 
    };
231
 
 
232
 
    this.addAttrs(attrs, cfg);
233
 
}
234
 
 
235
 
&#x2F;&#x2F; Augment custom class with Attribute 
236
 
Y.augment(MyClass, Y.Attribute);</pre>
237
 
 
238
 
 
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>
240
 
 
241
 
<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. 
243
 
</p>
244
 
 
245
 
<h3>Using Attributes</h3>
246
 
 
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>
248
 
 
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>
250
 
 
251
 
<pre class="code prettyprint">&#x2F;&#x2F; Create a new instance, but don&#x27;t provide any initial attribute values.
252
 
var o1 = new MyClass();
253
 
 
254
 
&#x2F;&#x2F; Display current values
255
 
displayValues(o1, &quot;o1 with default values, set during construction&quot;, 
256
 
                &quot;#createo1 .example-out&quot;);
257
 
 
258
 
...
259
 
 
260
 
&#x2F;&#x2F; Update values, using the &quot;set&quot; method
261
 
o1.set(&quot;foo&quot;, 10);
262
 
o1.set(&quot;bar&quot;, &quot;Hello New World!&quot;);
263
 
o1.set(&quot;foobar&quot;, false);
264
 
 
265
 
displayValues(o1, &quot;o1 values updated using set, after construction&quot;, 
266
 
                &quot;#updateo1 .example-out&quot;);</pre>
267
 
 
268
 
 
269
 
<p>For the second instance that, <code>o2</code> we set the initial values of the attributes, using the constructor configuration argument:</p>
270
 
 
271
 
<pre class="code prettyprint">var o2 = new MyClass({
272
 
    foo: 7,
273
 
    bar: &quot;Aloha World!&quot;,
274
 
    foobar: false
275
 
});</pre>
276
 
 
277
 
 
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>
279
 
 
280
 
<pre class="code prettyprint">function displayValues(o, title, node) {
281
 
    var str = 
282
 
        &#x27;&lt;div class=&quot;myclass&quot;&gt;&lt;div class=&quot;myclass-title&quot;&gt;&#x27; 
283
 
        + title + 
284
 
        &#x27;:&lt;&#x2F;div&gt;&lt;ul class=&quot;myclass-attrs&quot;&gt;&lt;li&gt;foo:&#x27; 
285
 
        + o.get(&quot;foo&quot;) 
286
 
        + &#x27;&lt;&#x2F;li&gt;&lt;li&gt;bar:&#x27;
287
 
        + o.get(&quot;bar&quot;)
288
 
        + &#x27;&lt;&#x2F;li&gt;&lt;li&gt;foobar:&#x27;
289
 
        + o.get(&quot;foobar&quot;)
290
 
        + &#x27;&lt;&#x2F;li&gt;&lt;&#x2F;ul&gt;&lt;&#x2F;div&gt;&#x27;;
291
 
 
292
 
    &#x2F;&#x2F; Use the Y.one() method to get the first element which 
293
 
    &#x2F;&#x2F; matches the selector passed in, to output the string to...       
294
 
    Y.one(node).set(&quot;innerHTML&quot;, str);
295
 
}</pre>
296
 
 
297
 
 
298
 
<h2>Complete Example Source</h2>
299
 
 
300
 
<pre class="code prettyprint">&lt;div id=&quot;createo1&quot;&gt;
301
 
    &lt;button type=&quot;button&quot; class=&quot;do&quot;&gt;Create First Instance&lt;&#x2F;button&gt; Construct o1, with default attribute values
302
 
    &lt;div class=&quot;example-out&quot;&gt;&lt;&#x2F;div&gt;
303
 
&lt;&#x2F;div&gt;
304
 
&lt;div id=&quot;updateo1&quot;&gt;
305
 
    &lt;button type=&quot;button&quot; class=&quot;do&quot;&gt;Update First Instance&lt;&#x2F;button&gt; Update the first instance, using set
306
 
    &lt;div class=&quot;example-out&quot;&gt;&lt;&#x2F;div&gt;
307
 
&lt;&#x2F;div&gt;
308
 
&lt;div id=&quot;createo2&quot;&gt;
309
 
    &lt;button type=&quot;button&quot; class=&quot;do&quot;&gt;Create Second Instance&lt;&#x2F;button&gt; Create the second instance, passing initial values to the constructor
310
 
    &lt;div class=&quot;example-out&quot;&gt;&lt;&#x2F;div&gt;
311
 
&lt;&#x2F;div&gt;
312
 
 
313
 
&lt;script type=&quot;text&#x2F;javascript&quot;&gt;
314
 
 
315
 
&#x2F;&#x2F; Get a new instance of YUI and 
316
 
&#x2F;&#x2F; load it with the required set of modules
317
 
 
318
 
YUI().use(&quot;node&quot;, &quot;attribute&quot;, function(Y) {
319
 
 
320
 
    &#x2F;&#x2F; Setup custom class which we want to 
321
 
    &#x2F;&#x2F; add managed attribute support to
322
 
 
323
 
    function MyClass(cfg) {
324
 
 
325
 
        &#x2F;&#x2F; When constructed, setup the initial attributes for the instance, by calling the addAttrs method.
326
 
        var attrs = {
327
 
            &#x2F;&#x2F; Add 3 attributes, foo, bar and foobar
328
 
            &quot;foo&quot; : {
329
 
                value:5
330
 
            },
331
 
     
332
 
            &quot;bar&quot; : {
333
 
                value:&quot;Hello World!&quot;
334
 
            },
335
 
    
336
 
            &quot;foobar&quot; : {
337
 
                value:true
338
 
            }
339
 
        };
340
 
 
341
 
        this.addAttrs(attrs, cfg);
342
 
    }
343
 
 
344
 
    &#x2F;&#x2F; Augment custom class with Attribute
345
 
    Y.augment(MyClass, Y.Attribute);
346
 
 
347
 
    function displayValues(o, title, node) {
348
 
        var str = 
349
 
            &#x27;&lt;div class=&quot;myclass&quot;&gt;&lt;div class=&quot;myclass-title&quot;&gt;&#x27; 
350
 
            + title + 
351
 
            &#x27;&lt;&#x2F;div&gt;&lt;ul class=&quot;myclass-attrs&quot;&gt;&lt;li&gt;foo: &#x27; 
352
 
            + o.get(&quot;foo&quot;) 
353
 
            + &#x27;&lt;&#x2F;li&gt;&lt;li&gt;bar: &#x27;
354
 
            + o.get(&quot;bar&quot;)
355
 
            + &#x27;&lt;&#x2F;li&gt;&lt;li&gt;foobar: &#x27;
356
 
            + o.get(&quot;foobar&quot;)
357
 
            + &#x27;&lt;&#x2F;li&gt;&lt;&#x2F;ul&gt;&lt;&#x2F;div&gt;&#x27;;
358
 
 
359
 
        Y.one(node).set(&quot;innerHTML&quot;, str);
360
 
    }
361
 
 
362
 
    Y.on(&quot;click&quot;, function() {
363
 
 
364
 
        &#x2F;&#x2F; Create a new instance, but don&#x27;t provide any initial attribute values.
365
 
        var o1 = new MyClass();
366
 
 
367
 
        &#x2F;&#x2F; Display current values
368
 
        displayValues(o1, &quot;o1 with default values, set during construction&quot;, &quot;#createo1 .example-out&quot;);
369
 
 
370
 
        Y.on(&quot;click&quot;, function() {
371
 
 
372
 
            &#x2F;&#x2F; Update values, using the &quot;set&quot; method
373
 
            o1.set(&quot;foo&quot;, 10);
374
 
            o1.set(&quot;bar&quot;, &quot;Hello New World!&quot;);
375
 
            o1.set(&quot;foobar&quot;, false);
376
 
 
377
 
            displayValues(o1, &quot;o1 values updated using set, after construction&quot;, &quot;#updateo1 .example-out&quot;);
378
 
 
379
 
        }, &quot;#updateo1 .do&quot;);
380
 
 
381
 
    }, &quot;#createo1 .do&quot;);
382
 
 
383
 
    Y.on(&quot;click&quot;, function() {
384
 
 
385
 
        var o2 = new MyClass({
386
 
            foo: 7,
387
 
            bar: &quot;Aloha World!&quot;,
388
 
            foobar: false
389
 
        });
390
 
 
391
 
        displayValues(o2, &quot;o2 values set during construction&quot;, &quot;#createo2 .example-out&quot;);
392
 
 
393
 
    }, &quot;#createo2 .do&quot;);
394
 
});
395
 
&lt;&#x2F;script&gt;</pre>
396
 
 
397
 
</div>
398
 
            </div>
399
 
        </div>
400
 
 
401
 
        <div class="yui3-u-1-4">
402
 
            <div class="sidebar">
403
 
                
404
 
 
405
 
                
406
 
                    <div class="sidebox">
407
 
                        <div class="hd">
408
 
                            <h2 class="no-toc">Examples</h2>
409
 
                        </div>
410
 
 
411
 
                        <div class="bd">
412
 
                            <ul class="examples">
413
 
                                
414
 
                                    
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>
417
 
                                        </li>
418
 
                                    
419
 
                                
420
 
                                    
421
 
                                        <li data-description="Configure attributes to be readOnly or writeOnce.">
422
 
                                            <a href="attribute-rw.html">Read-Only and Write-Once Attributes</a>
423
 
                                        </li>
424
 
                                    
425
 
                                
426
 
                                    
427
 
                                        <li data-description="How to listen for changes in attribute values.">
428
 
                                            <a href="attribute-event.html">Attribute Change Events</a>
429
 
                                        </li>
430
 
                                    
431
 
                                
432
 
                                    
433
 
                                        <li data-description="Create a basic SpeedDater class, with Attribute support.">
434
 
                                            <a href="attribute-basic-speeddate.html">Attribute Based Speed Dating</a>
435
 
                                        </li>
436
 
                                    
437
 
                                
438
 
                                    
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>
441
 
                                        </li>
442
 
                                    
443
 
                                
444
 
                                    
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>
447
 
                                        </li>
448
 
                                    
449
 
                                
450
 
                            </ul>
451
 
                        </div>
452
 
                    </div>
453
 
                
454
 
 
455
 
                
456
 
            </div>
457
 
        </div>
458
 
    </div>
459
 
</div>
460
 
 
461
 
<script src="../assets/vendor/prettify/prettify-min.js"></script>
462
 
<script>prettyPrint();</script>
463
 
 
464
 
</body>
465
 
</html>