|
1 |
|
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
3 <html> |
|
4 <head> |
|
5 <meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
6 <title>Basic Attribute Configuration</title> |
|
7 |
|
8 <style type="text/css"> |
|
9 /*margin and padding on body element |
|
10 can introduce errors in determining |
|
11 element position and are not recommended; |
|
12 we turn them off as a foundation for YUI |
|
13 CSS treatments. */ |
|
14 body { |
|
15 margin:0; |
|
16 padding:0; |
|
17 } |
|
18 </style> |
|
19 |
|
20 <link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
21 <script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
22 |
|
23 |
|
24 <!--begin custom header content for this example--> |
|
25 <style type="text/css"> |
|
26 .example-out .myclass-attrs { |
|
27 font-family:courier; |
|
28 margin-top:2px; |
|
29 } |
|
30 |
|
31 .example-out .myclass-title { |
|
32 font-weight:bold; |
|
33 font-family:arial; |
|
34 color:#8dd5e7; |
|
35 margin-top:5px; |
|
36 margin-bottom:3px; |
|
37 } |
|
38 |
|
39 .example-out { |
|
40 overflow:auto; |
|
41 border:1px solid #000; |
|
42 color:#ffffff; |
|
43 background-color:#004C6D; |
|
44 margin:5px; |
|
45 height:8em; |
|
46 padding:2px 2px 2px 5px; |
|
47 } |
|
48 </style> |
|
49 <!--end custom header content for this example--> |
|
50 |
|
51 </head> |
|
52 |
|
53 <body class=" yui-skin-sam"> |
|
54 |
|
55 <h1>Basic Attribute Configuration</h1> |
|
56 |
|
57 <div class="exampleIntro"> |
|
58 <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> |
|
59 |
|
60 <p> |
|
61 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="http://developer.yahoo.com/yui/3/base/index.html"><code>Base</code></a> class when you need managed attribute support, |
|
62 instead of augmenting Attribute directly, especially if you expect your class to be extended. <a href="http://developer.yahoo.com/yui/3/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. |
|
63 </p> |
|
64 </div> |
|
65 |
|
66 <!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
67 |
|
68 <div id="createo1"> |
|
69 <button type="button" class="do">Create First Instance</button> Construct o1, with default attribute values |
|
70 <div class="example-out"></div> |
|
71 </div> |
|
72 <div id="updateo1"> |
|
73 <button type="button" class="do">Update First Instance</button> Update the first instance, using set |
|
74 <div class="example-out"></div> |
|
75 </div> |
|
76 <div id="createo2"> |
|
77 <button type="button" class="do">Create Second Instance</button> Create the second instance, passing initial values to the constructor |
|
78 <div class="example-out"></div> |
|
79 </div> |
|
80 |
|
81 <script type="text/javascript"> |
|
82 |
|
83 // Get a new instance of YUI and |
|
84 // load it with the required set of modules |
|
85 |
|
86 YUI({base:"../../build/", timeout: 10000}).use("node", "attribute", function(Y) { |
|
87 |
|
88 // Setup custom class which we want to |
|
89 // add managed attribute support to |
|
90 |
|
91 function MyClass(cfg) { |
|
92 |
|
93 // When constructed, setup the initial attributes for the instance, by calling the addAttrs method. |
|
94 var attrs = { |
|
95 // Add 3 attributes, foo, bar and foobar |
|
96 "foo" : { |
|
97 value:5 |
|
98 }, |
|
99 |
|
100 "bar" : { |
|
101 value:"Hello World!" |
|
102 }, |
|
103 |
|
104 "foobar" : { |
|
105 value:true |
|
106 } |
|
107 }; |
|
108 |
|
109 this.addAttrs(attrs, cfg); |
|
110 } |
|
111 |
|
112 // Augment custom class with Attribute |
|
113 Y.augment(MyClass, Y.Attribute); |
|
114 |
|
115 function displayValues(o, title, node) { |
|
116 var str = |
|
117 '<div class="myclass"><div class="myclass-title">' |
|
118 + title + |
|
119 '</div><ul class="myclass-attrs"><li>foo: ' |
|
120 + o.get("foo") |
|
121 + '</li><li>bar: ' |
|
122 + o.get("bar") |
|
123 + '</li><li>foobar: ' |
|
124 + o.get("foobar") |
|
125 + '</li></ul></div>'; |
|
126 |
|
127 Y.one(node).set("innerHTML", str); |
|
128 } |
|
129 |
|
130 Y.on("click", function() { |
|
131 |
|
132 // Create a new instance, but don't provide any initial attribute values. |
|
133 var o1 = new MyClass(); |
|
134 |
|
135 // Display current values |
|
136 displayValues(o1, "o1 with default values, set during construction", "#createo1 .example-out"); |
|
137 |
|
138 Y.on("click", function() { |
|
139 |
|
140 // Update values, using the "set" method |
|
141 o1.set("foo", 10); |
|
142 o1.set("bar", "Hello New World!"); |
|
143 o1.set("foobar", false); |
|
144 |
|
145 displayValues(o1, "o1 values updated using set, after construction", "#updateo1 .example-out"); |
|
146 |
|
147 }, "#updateo1 .do"); |
|
148 |
|
149 }, "#createo1 .do"); |
|
150 |
|
151 Y.on("click", function() { |
|
152 |
|
153 var o2 = new MyClass({ |
|
154 foo: 7, |
|
155 bar: "Aloha World!", |
|
156 foobar: false |
|
157 }); |
|
158 |
|
159 displayValues(o2, "o2 values set during construction", "#createo2 .example-out"); |
|
160 |
|
161 }, "#createo2 .do"); |
|
162 }); |
|
163 </script> |
|
164 |
|
165 <!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
166 |
|
167 </body> |
|
168 </html> |