|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<title>YUI Library Examples: Attribute: Read-Only and Write-Once Attributes</title> |
|
|
6 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
7 |
<link rel="stylesheet" type="text/css" href="../../assets/yui.css" > |
|
|
8 |
|
|
|
9 |
<style> |
|
|
10 |
/*Supplemental CSS for the YUI distribution*/ |
|
|
11 |
#custom-doc { width: 95%; min-width: 950px; } |
|
|
12 |
#pagetitle {background-image: url(../../assets/bg_hd.gif);} |
|
|
13 |
/* #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}*/ |
|
|
14 |
</style> |
|
|
15 |
|
|
|
16 |
<link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css"> |
|
|
17 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
18 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
19 |
<style type="text/css"> |
|
|
20 |
.example-out .myclass-attrs { |
|
|
21 |
font-family:courier; |
|
|
22 |
margin-top:2px; |
|
|
23 |
} |
|
|
24 |
|
|
|
25 |
.example-out .myclass-title { |
|
|
26 |
font-weight:bold; |
|
|
27 |
font-family:arial; |
|
|
28 |
color:#8dd5e7; |
|
|
29 |
margin-top:5px; |
|
|
30 |
margin-bottom:3px; |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
.example-out { |
|
|
34 |
overflow:auto; |
|
|
35 |
border:1px solid #000; |
|
|
36 |
color:#ffffff; |
|
|
37 |
background-color:#004C6D; |
|
|
38 |
margin-top:5px; |
|
|
39 |
margin-bottom:20px; |
|
|
40 |
height:8em; |
|
|
41 |
padding:2px 2px 2px 5px; |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
#writeInitial label, #writeAgain label, #writeInternally label { |
|
|
45 |
font-weight:900; |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
#writeInitial .fooVal, #writeAgain .fooVal, #writeInternally .fooVal { |
|
|
49 |
width:9em; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
#writeInitial .barVal, #writeAgain .barVal, #writeInternally .barVal { |
|
|
53 |
width:9em; |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
#writeInitial p, #writeAgain p, #writeInternally p { |
|
|
57 |
margin-top:0.2em; |
|
|
58 |
margin-bottom:0.2em; |
|
|
59 |
color:#004C6D; |
|
|
60 |
font-size:108%; |
|
|
61 |
} |
|
|
62 |
</style> |
|
|
63 |
|
|
|
64 |
</head> |
|
|
65 |
<body id="yahoo-com" class=" yui-skin-sam"> |
|
|
66 |
<div id="custom-doc" class="yui-t2"> |
|
|
67 |
<div id="hd"> |
|
|
68 |
<div id="ygunav"> |
|
|
69 |
<p> |
|
|
70 |
<em> |
|
|
71 |
<a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> |
|
|
72 |
</em> |
|
|
73 |
</p> |
|
|
74 |
<form action="http://search.yahoo.com/search" id="sitesearchform"> |
|
|
75 |
<input name="vs" type="hidden" value="developer.yahoo.com"> |
|
|
76 |
<input name="vs" type="hidden" value="yuiblog.com"> |
|
|
77 |
<div id="sitesearch"> |
|
|
78 |
<label for="searchinput">Site Search (YDN & YUIBlog): </label> |
|
|
79 |
<input type="text" id="searchinput" name="p"> |
|
|
80 |
<input type="submit" value="Search" id="searchsubmit" class="ygbt"> |
|
|
81 |
</div> |
|
|
82 |
</form> |
|
|
83 |
</div> |
|
|
84 |
<div id="ygma"><a href="../../"><img src="../../assets/logo.gif" border="0" width="200" height="93"></a></div> |
|
|
85 |
<div id="pagetitle"><h1>YUI Library Examples: Attribute: Read-Only and Write-Once Attributes</h1></div> |
|
|
86 |
</div> |
|
|
87 |
<div id="bd"> |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
<div id="yui-main"> |
|
|
91 |
<div class="yui-b"> |
|
|
92 |
<div class="yui-ge"> |
|
|
93 |
<div class="yui-u first example" id="main"> |
|
|
94 |
|
|
|
95 |
<h2>Attribute: Read-Only and Write-Once Attributes</h2> |
|
|
96 |
|
|
|
97 |
<div id="example" class="promo"> |
|
|
98 |
<div class="example-intro"> |
|
|
99 |
Attributes can be configured to be <code>readOnly</code>, stopping them from being modified by the end user, or <code>writeOnce</code> allowing them to be set by the end user, but only once. This example demonstrates how to setup attributes for your class as <code>readOnly</code> or <code>writeOnce</code> attributes, and shows how their behavior differs when the end user attempts to set their values. |
|
|
100 |
</div> |
|
|
101 |
|
|
|
102 |
<div class="module example-container "> |
|
|
103 |
<div class="hd exampleHd"> |
|
|
104 |
<p class="newWindowButton yui-skin-sam"> |
|
|
105 |
<a href="attribute-rw_clean.html" target="_blank">View example in new window.</a> |
|
|
106 |
</p> |
|
|
107 |
</div> <div id="example-canvas" class="bd"> |
|
|
108 |
|
|
|
109 |
|
|
|
110 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
111 |
|
|
|
112 |
<div id="writeInitial"> |
|
|
113 |
<p>Construct o1, setting initial values for both foo and bar in the constructor: </p> |
|
|
114 |
<label>foo: <input type="text" value="Initial Foo" class="fooVal"></label> |
|
|
115 |
<label>bar: <input type="text" value="Initial Bar" class="barVal"></label> |
|
|
116 |
<button type="button" class="do">Initial Values</button> |
|
|
117 |
<div class="example-out"></div> |
|
|
118 |
</div> |
|
|
119 |
<div id="writeAgain"> |
|
|
120 |
<p>Try setting values again, after they've been set once: </p> |
|
|
121 |
<label>foo: <input type="text" value="Set Foo Again" class="fooVal"></label> |
|
|
122 |
<label>bar: <input type="text" value="Set Bar Again" class="barVal"></label> |
|
|
123 |
<button type="button" class="do">Set Again</button> |
|
|
124 |
<div class="example-out"></div> |
|
|
125 |
</div> |
|
|
126 |
<div id="writeInternally"> |
|
|
127 |
<p>Call a MyClass method, which sets foo internally (using _set): </p> |
|
|
128 |
<label>foo: <input type="text" value="Set Foo Internally" class="fooVal"></label> |
|
|
129 |
<button type="button" class="do">Set Internal</button> |
|
|
130 |
<div class="example-out"></div> |
|
|
131 |
</div> |
|
|
132 |
|
|
|
133 |
<script type="text/javascript"> |
|
|
134 |
YUI({base:"../../build/", timeout: 10000}).use("node", "attribute", function(Y) { |
|
|
135 |
|
|
|
136 |
// Setup a custom class with attribute support |
|
|
137 |
function MyClass(cfg) { |
|
|
138 |
|
|
|
139 |
var attrs = { |
|
|
140 |
"foo" : { |
|
|
141 |
value: "Default Foo", |
|
|
142 |
readOnly: true |
|
|
143 |
}, |
|
|
144 |
|
|
|
145 |
"bar" : { |
|
|
146 |
value: "Default Bar", |
|
|
147 |
writeOnce: true |
|
|
148 |
} |
|
|
149 |
} |
|
|
150 |
|
|
|
151 |
this.addAttrs(attrs, cfg); |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
MyClass.prototype.doSomething = function(val) { |
|
|
155 |
// ... Do something which requires |
|
|
156 |
// MyClass to change the value |
|
|
157 |
// of foo ... |
|
|
158 |
|
|
|
159 |
// Host code can reset value of |
|
|
160 |
// readOnly attributes interally, |
|
|
161 |
// by working with the private _set |
|
|
162 |
// property |
|
|
163 |
|
|
|
164 |
this._set("foo", val); |
|
|
165 |
}; |
|
|
166 |
|
|
|
167 |
Y.augment(MyClass, Y.Attribute); |
|
|
168 |
|
|
|
169 |
function displayValues(o, title, node) { |
|
|
170 |
var str = |
|
|
171 |
'<div class="myclass"><div class="myclass-title">' |
|
|
172 |
+ title + |
|
|
173 |
'</div><ul class="myclass-attrs"><li>foo (readOnly): ' |
|
|
174 |
+ o.get("foo") |
|
|
175 |
+ '</li><li>bar (writeOnce): ' |
|
|
176 |
+ o.get("bar") |
|
|
177 |
+ '</li></ul></div>'; |
|
|
178 |
|
|
|
179 |
Y.one(node).set("innerHTML", str); |
|
|
180 |
} |
|
|
181 |
|
|
|
182 |
var o1; |
|
|
183 |
|
|
|
184 |
Y.on("click", function() { |
|
|
185 |
|
|
|
186 |
var fooVal = Y.one("#writeInitial .fooVal").get("value"); |
|
|
187 |
var barVal = Y.one("#writeInitial .barVal").get("value"); |
|
|
188 |
|
|
|
189 |
o1 = new MyClass({ |
|
|
190 |
foo: fooVal, |
|
|
191 |
bar: barVal |
|
|
192 |
}); |
|
|
193 |
|
|
|
194 |
displayValues(o1, "Attempt to set initial values in constructor", "#writeInitial .example-out"); |
|
|
195 |
|
|
|
196 |
}, "#writeInitial .do"); |
|
|
197 |
|
|
|
198 |
Y.on("click", function() { |
|
|
199 |
|
|
|
200 |
if (o1) { |
|
|
201 |
var fooVal = Y.one("#writeAgain .fooVal").get("value"); |
|
|
202 |
var barVal = Y.one("#writeAgain .barVal").get("value"); |
|
|
203 |
|
|
|
204 |
// Attempt to reset values: |
|
|
205 |
o1.set("foo", fooVal); |
|
|
206 |
o1.set("bar", barVal); |
|
|
207 |
|
|
|
208 |
displayValues(o1, "Attempt to set values again, using set", "#writeAgain .example-out"); |
|
|
209 |
} |
|
|
210 |
|
|
|
211 |
}, "#writeAgain .do"); |
|
|
212 |
|
|
|
213 |
Y.on("click", function() { |
|
|
214 |
|
|
|
215 |
if (o1) { |
|
|
216 |
var val = Y.one("#writeInternally .fooVal").get("value"); |
|
|
217 |
// Call method, which sets foo internally... |
|
|
218 |
o1.doSomething(val); |
|
|
219 |
|
|
|
220 |
displayValues(o1, "Set the value of foo (readOnly) internally", "#writeInternally .example-out"); |
|
|
221 |
} |
|
|
222 |
|
|
|
223 |
}, "#writeInternally .do"); |
|
|
224 |
}); |
|
|
225 |
</script> |
|
|
226 |
|
|
|
227 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
228 |
|
|
|
229 |
|
|
|
230 |
</div> |
|
|
231 |
</div> |
|
|
232 |
</div> |
|
|
233 |
|
|
|
234 |
<h3>ReadOnly And WriteOnce</h3> |
|
|
235 |
|
|
|
236 |
<p>Attribute supports the ability to configure attributes to be <code>readOnly</code> or <code>writeOnce</code>. <code>readOnly</code> attributes cannot be set by the end user, either through initial values passed to the constructor, or by invoking the <code>set</code> method. <code>writeOnce</code> attributes on the other hand, can be set by the user, but only once, either during initialization or through a call to <code>set</code>. Once a value is established for a <code>writeOnce</code> attribute, it cannot be reset to another value by the user.</p> |
|
|
237 |
|
|
|
238 |
<h3>Configuring ReadOnly And WriteOnce Attributes</h3> |
|
|
239 |
|
|
|
240 |
<p>This example sets up a custom class, <code>MyClass</code>, with two attributes, <code>foo</code> and <code>bar</code>. <code>foo</code> is configured to be a <code>readOnly</code> attribute, and <code>bar</code> is configured to be a <code>writeOnce</code> attribute:</p> |
|
|
241 |
|
|
|
242 |
<div id="syntax1" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="co1">// Setup a custom class with attribute support</span></div></li><li class="li1"><div class="de1"><span class="kw2">function</span> MyClass<span class="br0">(</span>cfg<span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">// Attribute configuration</span></div></li><li class="li2"><div class="de2"> <span class="kw2">var</span> attrs <span class="sy0">=</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> <span class="st0">"foo"</span> <span class="sy0">:</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> value<span class="sy0">:</span> <span class="st0">"Default Foo"</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> readOnly<span class="sy0">:</span> <span class="kw2">true</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">,</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="st0">"bar"</span> <span class="sy0">:</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> value<span class="sy0">:</span> <span class="st0">"Default Bar"</span><span class="sy0">,</span></div></li><li class="li1"><div class="de1"> writeOnce<span class="sy0">:</span> <span class="kw2">true</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span></div></li><li class="li2"><div class="de2"> <span class="br0">}</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="kw1">this</span>.<span class="me1">addAttrs</span><span class="br0">(</span>attrs<span class="sy0">,</span> cfg<span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="br0">}</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;"><span class="co1">// Setup a custom class with attribute support</span> |
|
|
243 |
<span class="kw2">function</span> MyClass<span class="br0">(</span>cfg<span class="br0">)</span> <span class="br0">{</span> |
|
|
244 |
|
|
|
245 |
<span class="co1">// Attribute configuration</span> |
|
|
246 |
<span class="kw2">var</span> attrs <span class="sy0">=</span> <span class="br0">{</span> |
|
|
247 |
<span class="st0">"foo"</span> <span class="sy0">:</span> <span class="br0">{</span> |
|
|
248 |
value<span class="sy0">:</span> <span class="st0">"Default Foo"</span><span class="sy0">,</span> |
|
|
249 |
readOnly<span class="sy0">:</span> <span class="kw2">true</span> |
|
|
250 |
<span class="br0">}</span><span class="sy0">,</span> |
|
|
251 |
|
|
|
252 |
<span class="st0">"bar"</span> <span class="sy0">:</span> <span class="br0">{</span> |
|
|
253 |
value<span class="sy0">:</span> <span class="st0">"Default Bar"</span><span class="sy0">,</span> |
|
|
254 |
writeOnce<span class="sy0">:</span> <span class="kw2">true</span> |
|
|
255 |
<span class="br0">}</span> |
|
|
256 |
<span class="br0">}</span> |
|
|
257 |
|
|
|
258 |
<span class="kw1">this</span>.<span class="me1">addAttrs</span><span class="br0">(</span>attrs<span class="sy0">,</span> cfg<span class="br0">)</span><span class="sy0">;</span> |
|
|
259 |
<span class="br0">}</span></pre></div><textarea id="syntax1-plain">// Setup a custom class with attribute support |
|
|
260 |
function MyClass(cfg) { |
|
|
261 |
|
|
|
262 |
// Attribute configuration |
|
|
263 |
var attrs = { |
|
|
264 |
"foo" : { |
|
|
265 |
value: "Default Foo", |
|
|
266 |
readOnly: true |
|
|
267 |
}, |
|
|
268 |
|
|
|
269 |
"bar" : { |
|
|
270 |
value: "Default Bar", |
|
|
271 |
writeOnce: true |
|
|
272 |
} |
|
|
273 |
} |
|
|
274 |
|
|
|
275 |
this.addAttrs(attrs, cfg); |
|
|
276 |
}</textarea></div> |
|
|
277 |
|
|
|
278 |
<h3>Attempting To Set Values</h3> |
|
|
279 |
|
|
|
280 |
<p>We first attempt to set values for both attributes in the constructor (used to intialize the attributes) and see that only <code>bar</code>, the <code>writeOnce</code> attribute, gets set to the user provided value:</p> |
|
|
281 |
|
|
|
282 |
<div id="syntax2" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw2">var</span> fooVal <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeInitial .fooVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="kw2">var</span> barVal <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeInitial .barVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">o1 <span class="sy0">=</span> <span class="kw2">new</span> MyClass<span class="br0">(</span><span class="br0">{</span></div></li><li class="li2"><div class="de2"> foo<span class="sy0">:</span> fooVal<span class="sy0">,</span></div></li><li class="li1"><div class="de1"> bar<span class="sy0">:</span> barVal</div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">displayValues<span class="br0">(</span>o1<span class="sy0">,</span> <span class="st0">"Attempt to set initial values in constructor"</span><span class="sy0">,</span> </div></li><li class="li2"><div class="de2"> <span class="st0">"#writeInitial .example-out"</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> fooVal <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeInitial .fooVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
283 |
<span class="kw2">var</span> barVal <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeInitial .barVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
284 |
|
|
|
285 |
o1 <span class="sy0">=</span> <span class="kw2">new</span> MyClass<span class="br0">(</span><span class="br0">{</span> |
|
|
286 |
foo<span class="sy0">:</span> fooVal<span class="sy0">,</span> |
|
|
287 |
bar<span class="sy0">:</span> barVal |
|
|
288 |
<span class="br0">}</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
289 |
|
|
|
290 |
displayValues<span class="br0">(</span>o1<span class="sy0">,</span> <span class="st0">"Attempt to set initial values in constructor"</span><span class="sy0">,</span> |
|
|
291 |
<span class="st0">"#writeInitial .example-out"</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax2-plain">var fooVal = Y.one("#writeInitial .fooVal").get("value"); |
|
|
292 |
var barVal = Y.one("#writeInitial .barVal").get("value"); |
|
|
293 |
|
|
|
294 |
o1 = new MyClass({ |
|
|
295 |
foo: fooVal, |
|
|
296 |
bar: barVal |
|
|
297 |
}); |
|
|
298 |
|
|
|
299 |
displayValues(o1, "Attempt to set initial values in constructor", |
|
|
300 |
"#writeInitial .example-out");</textarea></div> |
|
|
301 |
<p>We then attempt to set values for both attributes again, using <code>set</code>, and see that niether of the values are modified:</p> |
|
|
302 |
|
|
|
303 |
<div id="syntax3" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw2">var</span> fooVal <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeAgain .fooVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="kw2">var</span> barVal <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeAgain .barVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"><span class="co1">// Attempt to reset values:</span></div></li><li class="li2"><div class="de2">o1.<span class="me1">set</span><span class="br0">(</span><span class="st0">"foo"</span><span class="sy0">,</span> fooVal<span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">o1.<span class="me1">set</span><span class="br0">(</span><span class="st0">"bar"</span><span class="sy0">,</span> barVal<span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">displayValues<span class="br0">(</span>o1<span class="sy0">,</span> <span class="st0">"Attempt to set values again, using set"</span><span class="sy0">,</span> </div></li><li class="li1"><div class="de1"> <span class="st0">"#writeAgain .example-out"</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> fooVal <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeAgain .fooVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
304 |
<span class="kw2">var</span> barVal <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeAgain .barVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
305 |
|
|
|
306 |
<span class="co1">// Attempt to reset values:</span> |
|
|
307 |
o1.<span class="me1">set</span><span class="br0">(</span><span class="st0">"foo"</span><span class="sy0">,</span> fooVal<span class="br0">)</span><span class="sy0">;</span> |
|
|
308 |
o1.<span class="me1">set</span><span class="br0">(</span><span class="st0">"bar"</span><span class="sy0">,</span> barVal<span class="br0">)</span><span class="sy0">;</span> |
|
|
309 |
|
|
|
310 |
displayValues<span class="br0">(</span>o1<span class="sy0">,</span> <span class="st0">"Attempt to set values again, using set"</span><span class="sy0">,</span> |
|
|
311 |
<span class="st0">"#writeAgain .example-out"</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax3-plain">var fooVal = Y.one("#writeAgain .fooVal").get("value"); |
|
|
312 |
var barVal = Y.one("#writeAgain .barVal").get("value"); |
|
|
313 |
|
|
|
314 |
// Attempt to reset values: |
|
|
315 |
o1.set("foo", fooVal); |
|
|
316 |
o1.set("bar", barVal); |
|
|
317 |
|
|
|
318 |
displayValues(o1, "Attempt to set values again, using set", |
|
|
319 |
"#writeAgain .example-out");</textarea></div> |
|
|
320 |
<h3>Setting The State Of ReadOnly Values Internally</h3> |
|
|
321 |
|
|
|
322 |
<p>Although the user cannot update the value of <code>readOnly</code> attributes, it maybe neccessary for the host object to update it's value internally. The example shows how this can be done, using the private <code>_set</code> property on the host:</p> |
|
|
323 |
|
|
|
324 |
<div id="syntax4" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1">MyClass.<span class="me1">prototype</span>.<span class="me1">doSomething</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">(</span>val<span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> <span class="co1">// ... Do something which requires</span></div></li><li class="li1"><div class="de1"> <span class="co1">// MyClass to change the value</span></div></li><li class="li1"><div class="de1"> <span class="co1">// of foo ...</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="co1">// Host code can reset value of </span></div></li><li class="li1"><div class="de1"> <span class="co1">// readOnly attributes interally,</span></div></li><li class="li1"><div class="de1"> <span class="co1">// by working with the private _set</span></div></li><li class="li1"><div class="de1"> <span class="co1">// property</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="kw1">this</span>._set<span class="br0">(</span><span class="st0">"foo"</span><span class="sy0">,</span> val<span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">...</div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"><span class="kw2">var</span> val <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeInternally .fooVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"><span class="co1">// Call method, which sets foo internally...</span></div></li><li class="li1"><div class="de1">o1.<span class="me1">doSomething</span><span class="br0">(</span>val<span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1">displayValues<span class="br0">(</span>o1<span class="sy0">,</span> <span class="st0">"Set value of foo (readOnly) interally"</span><span class="sy0">,</span> </div></li><li class="li1"><div class="de1"> <span class="st0">"#writeInternally .example-out"</span><span class="br0">)</span><span class="sy0">;</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;">MyClass.<span class="me1">prototype</span>.<span class="me1">doSomething</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">(</span>val<span class="br0">)</span> <span class="br0">{</span> |
|
|
325 |
<span class="co1">// ... Do something which requires</span> |
|
|
326 |
<span class="co1">// MyClass to change the value</span> |
|
|
327 |
<span class="co1">// of foo ...</span> |
|
|
328 |
|
|
|
329 |
<span class="co1">// Host code can reset value of </span> |
|
|
330 |
<span class="co1">// readOnly attributes interally,</span> |
|
|
331 |
<span class="co1">// by working with the private _set</span> |
|
|
332 |
<span class="co1">// property</span> |
|
|
333 |
|
|
|
334 |
<span class="kw1">this</span>._set<span class="br0">(</span><span class="st0">"foo"</span><span class="sy0">,</span> val<span class="br0">)</span><span class="sy0">;</span> |
|
|
335 |
<span class="br0">}</span><span class="sy0">;</span> |
|
|
336 |
|
|
|
337 |
... |
|
|
338 |
|
|
|
339 |
<span class="kw2">var</span> val <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">"#writeInternally .fooVal"</span><span class="br0">)</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">"value"</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
340 |
|
|
|
341 |
<span class="co1">// Call method, which sets foo internally...</span> |
|
|
342 |
o1.<span class="me1">doSomething</span><span class="br0">(</span>val<span class="br0">)</span><span class="sy0">;</span> |
|
|
343 |
|
|
|
344 |
displayValues<span class="br0">(</span>o1<span class="sy0">,</span> <span class="st0">"Set value of foo (readOnly) interally"</span><span class="sy0">,</span> |
|
|
345 |
<span class="st0">"#writeInternally .example-out"</span><span class="br0">)</span><span class="sy0">;</span></pre></div><textarea id="syntax4-plain">MyClass.prototype.doSomething = function(val) { |
|
|
346 |
// ... Do something which requires |
|
|
347 |
// MyClass to change the value |
|
|
348 |
// of foo ... |
|
|
349 |
|
|
|
350 |
// Host code can reset value of |
|
|
351 |
// readOnly attributes interally, |
|
|
352 |
// by working with the private _set |
|
|
353 |
// property |
|
|
354 |
|
|
|
355 |
this._set("foo", val); |
|
|
356 |
}; |
|
|
357 |
|
|
|
358 |
... |
|
|
359 |
|
|
|
360 |
var val = Y.one("#writeInternally .fooVal").get("value"); |
|
|
361 |
|
|
|
362 |
// Call method, which sets foo internally... |
|
|
363 |
o1.doSomething(val); |
|
|
364 |
|
|
|
365 |
displayValues(o1, "Set value of foo (readOnly) interally", |
|
|
366 |
"#writeInternally .example-out");</textarea></div> </div> |
|
|
367 |
<div class="yui-u sidebar"> |
|
|
368 |
|
|
|
369 |
|
|
|
370 |
<div id="examples" class="mod box4"> |
|
|
371 |
<div class="hd"> |
|
|
372 |
<h4> |
|
|
373 |
Attribute Examples:</h4> |
|
|
374 |
</div> |
|
|
375 |
<div class="bd"> |
|
|
376 |
<ul> |
|
|
377 |
<li><a href='../attribute/attribute-basic.html'>Basic Attribute Configuration</a></li><li class='selected'><a href='../attribute/attribute-rw.html'>Read-Only and Write-Once Attributes</a></li><li><a href='../attribute/attribute-event.html'>Attribute Change Events</a></li><li><a href='../attribute/attribute-basic-speeddate.html'>Attribute Based Speed Dating</a></li><li><a href='../attribute/attribute-event-speeddate.html'>Attribute Event Based Speed Dating</a></li><li><a href='../attribute/attribute-getset.html'>Attribute Getters, Setters and Validators</a></li> </ul> |
|
|
378 |
</div> |
|
|
379 |
</div> |
|
|
380 |
|
|
|
381 |
<div class="mod box4"> |
|
|
382 |
<div class="hd"> |
|
|
383 |
<h4>More Attribute Resources:</h4> |
|
|
384 |
</div> |
|
|
385 |
<div class="bd"> |
|
|
386 |
<ul> |
|
|
387 |
<!-- <li><a href="http://developer.yahoo.com/yui/attribute/">User's Guide</a> (external)</li> --> |
|
|
388 |
<li><a href="../../api/module_attribute.html">API Documentation</a></li></ul> |
|
|
389 |
</div> |
|
|
390 |
</div> |
|
|
391 |
</div> |
|
|
392 |
</div> |
|
|
393 |
|
|
|
394 |
</div> |
|
|
395 |
</div> |
|
|
396 |
|
|
|
397 |
|
|
|
398 |
<div class="yui-b toc3" id="tocWrapper"> |
|
|
399 |
<!-- TABLE OF CONTENTS --> |
|
|
400 |
<div id="toc"> |
|
|
401 |
|
|
|
402 |
<ul> |
|
|
403 |
<li class="sect first">YUI 3 Resources</li><li class="item"><a title="YUI 3 -- Yahoo! User Interface (YUI) Library" href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site</a></li><li class="item"><a title="Examples of every YUI utility and control in action" href="../../examples/">YUI 3 Examples</a></li><li class="item"><a title="Instantly searchable API documentation for the entire YUI library." href="../../api/">YUI 3 API Docs</a></li><li class="item"><a title="YUI 3 Dependency Configurator -- configure your custom YUI implementation" href="http://developer.yahoo.com/yui/3/configurator">YUI 3 Dependency Configurator</a></li><li class="item"><a title="The YUI 3 Forum on YUILibrary.com" href="http://yuilibrary.com/forum/viewforum.php?f=15">YUI 3 Forums (external)</a></li><li class="item"><a title="Found a bug or a missing feature? Let us know on YUILibrary.com." href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/license.html">YUI License</a></li><li class="item"><a title="Download and fork the YUI project on GitHub" href="http://github.com/yui">YUI on Github</a></li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="item"><a title="YUI Global Object - Functional Examples" href="../../examples/yui/index.html">YUI Global Object</a></li><li class="item"><a title="Event - Functional Examples" href="../../examples/event/index.html">Event</a></li><li class="item"><a title="Node - Functional Examples" href="../../examples/node/index.html">Node</a></li><li class="sect">YUI 3 Component Infrastructure - Examples</li><li class="selected "><a title="Attribute - Functional Examples" href="../../examples/attribute/index.html">Attribute</a></li><li class="item"><a title="Plugin - Functional Examples" href="../../examples/plugin/index.html">Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Widget - Functional Examples" href="../../examples/widget/index.html">Widget <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 Utilities - Examples</li><li class="item"><a title="Animation - Functional Examples" href="../../examples/anim/index.html">Animation</a></li><li class="item"><a title="AsyncQueue - Functional Examples" href="../../examples/async-queue/index.html">AsyncQueue</a></li><li class="item"><a title="Browser History - Functional Examples" href="../../examples/history/index.html">Browser History</a></li><li class="item"><a title="Cache - Functional Examples" href="../../examples/cache/index.html">Cache</a></li><li class="item"><a title="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="DataSchema - Functional Examples" href="../../examples/dataschema/index.html">DataSchema <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="DataSource - Functional Examples" href="../../examples/datasource/index.html">DataSource <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="DataType - Functional Examples" href="../../examples/datatype/index.html">DataType <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Drag & Drop - Functional Examples" href="../../examples/dd/index.html">Drag & Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</a></li><li class="item"><a title="ImageLoader - Functional Examples" href="../../examples/imageloader/index.html">ImageLoader</a></li><li class="item"><a title="IO - Functional Examples" href="../../examples/io/index.html">IO</a></li><li class="item"><a title="JSON (JavaScript Object Notation) - Functional Examples" href="../../examples/json/index.html">JSON</a></li><li class="item"><a title="Stylesheet - Functional Examples" href="../../examples/stylesheet/index.html">Stylesheet</a></li><li class="sect">YUI 3 Widgets - Examples</li><li class="item"><a title="Overlay - Functional Examples" href="../../examples/overlay/index.html">Overlay <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Slider - Functional Examples" href="../../examples/slider/index.html">Slider <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 Node Plugins - Examples</li><li class="item"><a title="FocusManager Node Plugin - Functional Examples" href="../../examples/node-focusmanager/index.html">FocusManager Node Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="MenuNav Node Plugin - Functional Examples" href="../../examples/node-menunav/index.html">MenuNav Node Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 CSS - Examples</li><li class="item"><a title="YUI CSS Reset - Functional Examples" href="../../examples/cssreset/index.html">CSS Reset</a></li><li class="item"><a title="YUI Fonts - Functional Examples" href="../../examples/cssfonts/index.html">CSS Fonts</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</a></li><li class="sect">YUI 3 Developer Tools - Examples</li><li class="item"><a title="Console - Functional Examples" href="../../examples/console/index.html">Console <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Console Filters Plugin- Functional Examples" href="../../examples/console-filters/index.html">Plugin.ConsoleFilters <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Profiler - Functional Examples" href="../../examples/profiler/index.html">Profiler</a></li><li class="item"><a title="Test - Functional Examples" href="../../examples/test/index.html">Test</a></li><li class="sect">Other Useful YUI 3 Resources</li><li class="item"><a title="Answers to Frequently Asked Questions about the YUI Library" href="http://developer.yahoo.com/yui/articles/faq/">YUI FAQ (external)</a></li><li class="item"><a title="Yahoo!'s philosophy of Graded Browser Support" href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support (external)</a></li><li class="item"><a title="Videos and podcasts from the YUI Team and from the Yahoo! frontend engineering community." href="http://developer.yahoo.com/yui/theater/">YUI Theater (external)</a></li></ul> |
|
|
404 |
</div> |
|
|
405 |
</div> |
|
|
406 |
</div><!--closes bd--> |
|
|
407 |
|
|
|
408 |
<div id="ft"> |
|
|
409 |
<p class="first">Copyright © 2009 Yahoo! Inc. All rights reserved.</p> |
|
|
410 |
<p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> - |
|
|
411 |
<a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> - |
|
|
412 |
<a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> - |
|
|
413 |
<a href="http://careers.yahoo.com/">Job Openings</a></p> |
|
|
414 |
</div> |
|
|
415 |
</div> |
|
|
416 |
<script language="javascript"> |
|
|
417 |
var yuiConfig = {base:"../../build/", timeout: 10000}; |
|
|
418 |
</script> |
|
|
419 |
<script src="../../assets/syntax.js"></script> |
|
|
420 |
<script src="../../assets/dpSyntaxHighlighter.js"></script> |
|
|
421 |
<script language="javascript"> |
|
|
422 |
dp.SyntaxHighlighter.HighlightAll('code'); |
|
|
423 |
</script> |
|
|
424 |
</body> |
|
|
425 |
</html> |