|
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: readOnly and writeOnce Configuration</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 .entry { |
|
|
21 |
font-family:courier; |
|
|
22 |
margin-top:2px; |
|
|
23 |
margin-left:10px; |
|
|
24 |
} |
|
|
25 |
|
|
|
26 |
#example-out .header { |
|
|
27 |
font-weight:bold; |
|
|
28 |
font-family:arial; |
|
|
29 |
color:#8dd5e7; |
|
|
30 |
margin-top:10px; |
|
|
31 |
margin-bottom:3px; |
|
|
32 |
margin-left:0px; |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
#example-out .subheader { |
|
|
36 |
font-weight:bold; |
|
|
37 |
font-family:arial; |
|
|
38 |
color:#EDFF9F; |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
#example-out { |
|
|
42 |
margin:5px; |
|
|
43 |
border:1px solid #000; |
|
|
44 |
color:#ffffff; |
|
|
45 |
background-color:#004c6d; |
|
|
46 |
overflow:auto; |
|
|
47 |
width:30em; |
|
|
48 |
height:22em; |
|
|
49 |
padding:2px 5px; |
|
|
50 |
} |
|
|
51 |
</style> |
|
|
52 |
<script type="text/javascript"> |
|
|
53 |
YUI.namespace("example"); |
|
|
54 |
YUI.example.print = function(msg, cl) { |
|
|
55 |
var o = document.getElementById("example-out"); |
|
|
56 |
if (o) { |
|
|
57 |
cl = cl || ""; |
|
|
58 |
o.innerHTML += '<div class="entry ' + cl + '">' + msg + '</div>'; |
|
|
59 |
} |
|
|
60 |
} |
|
|
61 |
</script> |
|
|
62 |
|
|
|
63 |
</head> |
|
|
64 |
<body id="yahoo-com" class=" yui-skin-sam"> |
|
|
65 |
<div id="custom-doc" class="yui-t2"> |
|
|
66 |
<div id="hd"> |
|
|
67 |
<div id="ygunav"> |
|
|
68 |
<p> |
|
|
69 |
<em> |
|
|
70 |
<a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> |
|
|
71 |
</em> |
|
|
72 |
</p> |
|
|
73 |
<form action="http://search.yahoo.com/search" id="sitesearchform"> |
|
|
74 |
<input name="vs" type="hidden" value="developer.yahoo.com"> |
|
|
75 |
<input name="vs" type="hidden" value="yuiblog.com"> |
|
|
76 |
<div id="sitesearch"> |
|
|
77 |
<label for="searchinput">Site Search (YDN & YUIBlog): </label> |
|
|
78 |
<input type="text" id="searchinput" name="p"> |
|
|
79 |
<input type="submit" value="Search" id="searchsubmit" class="ygbt"> |
|
|
80 |
</div> |
|
|
81 |
</form> |
|
|
82 |
</div> |
|
|
83 |
<div id="ygma"><a href="../../"><img src="../../assets/logo.gif" border="0" width="200" height="93"></a></div> |
|
|
84 |
<div id="pagetitle"><h1>YUI Library Examples: Attribute: readOnly and writeOnce Configuration</h1></div> |
|
|
85 |
</div> |
|
|
86 |
<div id="bd"> |
|
|
87 |
|
|
|
88 |
<div id="bar-note"><p><strong>Note:</strong> This is YUI 3.x. Looking for <a href="http://developer.yahoo.com/yui/">YUI 2.x</a>?</p></div> |
|
|
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: readOnly and writeOnce Configuration</h2> |
|
|
96 |
|
|
|
97 |
<div id="example" class="promo"> |
|
|
98 |
<p> |
|
|
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 only 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 |
</p> |
|
|
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="example-out"></div> |
|
|
113 |
|
|
|
114 |
<script type="text/javascript"> |
|
|
115 |
YUI({base:"../../build/", timeout: 10000}).use("node", "attribute", function(Y) { |
|
|
116 |
|
|
|
117 |
// Shortcut to print (unrelated to example) |
|
|
118 |
var print = YUI.example.print; |
|
|
119 |
|
|
|
120 |
// Setup a custom class with attribute support |
|
|
121 |
function MyClass(cfg) { |
|
|
122 |
this.addAttrs(Y.merge(MyClass.ATTRIBUTES), cfg); |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
// Setup attribute configuration |
|
|
126 |
MyClass.ATTRIBUTES = { |
|
|
127 |
"foo" : { |
|
|
128 |
value: "Default Foo", |
|
|
129 |
readOnly: true |
|
|
130 |
}, |
|
|
131 |
|
|
|
132 |
"bar" : { |
|
|
133 |
value: "Default Bar", |
|
|
134 |
writeOnce: true |
|
|
135 |
} |
|
|
136 |
}; |
|
|
137 |
|
|
|
138 |
MyClass.prototype.doSomething = function() { |
|
|
139 |
// ... Do something which requires |
|
|
140 |
// MyClass to change the value |
|
|
141 |
// of foo ... |
|
|
142 |
|
|
|
143 |
// Host code can reset value of |
|
|
144 |
// readOnly attributes interally, |
|
|
145 |
// by working with the private _set |
|
|
146 |
// property |
|
|
147 |
|
|
|
148 |
this._set("foo", "New Default Foo"); |
|
|
149 |
}; |
|
|
150 |
|
|
|
151 |
Y.augment(MyClass, Y.Attribute); |
|
|
152 |
|
|
|
153 |
var o1 = new MyClass({ |
|
|
154 |
foo: "User Foo", |
|
|
155 |
bar: "User Bar" |
|
|
156 |
}); |
|
|
157 |
|
|
|
158 |
print("Initial Set", "header"); |
|
|
159 |
print("foo value (readOnly):", "subheader"); |
|
|
160 |
print(o1.get("foo")); |
|
|
161 |
|
|
|
162 |
print("bar value (writeOnce):", "subheader"); |
|
|
163 |
print(o1.get("bar")); |
|
|
164 |
|
|
|
165 |
// Attempt to reset values: |
|
|
166 |
|
|
|
167 |
print("Try Setting Again", "header"); |
|
|
168 |
o1.set("foo", "User Reset Foo"); |
|
|
169 |
o1.set("bar", "User Reset Bar"); |
|
|
170 |
|
|
|
171 |
print("Final foo value (readOnly)", "subheader"); |
|
|
172 |
print(o1.get("foo")); |
|
|
173 |
|
|
|
174 |
print("Final bar value (writeOnce)", "subheader"); |
|
|
175 |
print(o1.get("bar")); |
|
|
176 |
|
|
|
177 |
print("Set Value Of Foo Interally", "header"); |
|
|
178 |
print("foo value (readOnly):", "subheader"); |
|
|
179 |
|
|
|
180 |
o1.doSomething(); |
|
|
181 |
print(o1.get("foo")); |
|
|
182 |
|
|
|
183 |
}); |
|
|
184 |
</script> |
|
|
185 |
|
|
|
186 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
187 |
|
|
|
188 |
|
|
|
189 |
</div> |
|
|
190 |
</div> |
|
|
191 |
</div> |
|
|
192 |
|
|
|
193 |
<h3>ReadOnly And WriteOnce</h3> |
|
|
194 |
|
|
|
195 |
<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> |
|
|
196 |
|
|
|
197 |
<h3>Configuring ReadOnly And WriteOnce Attributes</h3> |
|
|
198 |
|
|
|
199 |
<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> |
|
|
200 |
|
|
|
201 |
<textarea name="code" class="JScript" rows="1" cols="60"> |
|
|
202 |
// Setup a custom class with attribute support |
|
|
203 |
function MyClass(cfg) { |
|
|
204 |
this.addAttrs(Y.merge(MyClass.ATTRIBUTES), cfg); |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
// Setup attribute configuration |
|
|
208 |
MyClass.ATTRIBUTES = { |
|
|
209 |
"foo" : { |
|
|
210 |
value: "Default Foo", |
|
|
211 |
readOnly: true |
|
|
212 |
}, |
|
|
213 |
|
|
|
214 |
"bar" : { |
|
|
215 |
value: "Default Bar", |
|
|
216 |
writeOnce: true |
|
|
217 |
} |
|
|
218 |
}; |
|
|
219 |
</textarea> |
|
|
220 |
|
|
|
221 |
<h3>Attempting To Set Values</h3> |
|
|
222 |
|
|
|
223 |
<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> |
|
|
224 |
|
|
|
225 |
<textarea name="code" class="JScript" rows="1" cols="60"> |
|
|
226 |
var o1 = new MyClass({ |
|
|
227 |
foo: "User Foo", |
|
|
228 |
bar: "User Bar" |
|
|
229 |
}); |
|
|
230 |
|
|
|
231 |
print("Initial Set", "header"); |
|
|
232 |
print("foo value (readOnly):", "subheader"); |
|
|
233 |
print(o1.get("foo")); |
|
|
234 |
|
|
|
235 |
print("bar value (writeOnce):", "subheader"); |
|
|
236 |
print(o1.get("bar")); |
|
|
237 |
</textarea> |
|
|
238 |
|
|
|
239 |
<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> |
|
|
240 |
|
|
|
241 |
<textarea name="code" class="JScript" rows="1" cols="60"> |
|
|
242 |
print("Try Setting Again", "header"); |
|
|
243 |
o1.set("foo", "User Reset Foo"); |
|
|
244 |
o1.set("bar", "User Reset Bar"); |
|
|
245 |
|
|
|
246 |
print("Final foo value (readOnly)", "subheader"); |
|
|
247 |
print(o1.get("foo")); |
|
|
248 |
|
|
|
249 |
print("Final bar value (writeOnce)", "subheader"); |
|
|
250 |
print(o1.get("bar")); |
|
|
251 |
</textarea> |
|
|
252 |
|
|
|
253 |
<h3>Setting The State Of ReadOnly Values Internally</h3> |
|
|
254 |
|
|
|
255 |
<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> |
|
|
256 |
|
|
|
257 |
<textarea name="code" class="JScript" rows="1" cols="60"> |
|
|
258 |
MyClass.prototype.doSomething = function() { |
|
|
259 |
// ... Do something which requires |
|
|
260 |
// MyClass to change the value |
|
|
261 |
// of foo ... |
|
|
262 |
|
|
|
263 |
// Host code can reset value of |
|
|
264 |
// readOnly attributes interally, |
|
|
265 |
// by working with the private _set property |
|
|
266 |
|
|
|
267 |
this._set("foo", "New Default Foo"); |
|
|
268 |
}; |
|
|
269 |
</textarea> |
|
|
270 |
</div> |
|
|
271 |
<div class="yui-u sidebar"> |
|
|
272 |
|
|
|
273 |
|
|
|
274 |
<div id="examples" class="mod box4"> |
|
|
275 |
<div class="hd"> |
|
|
276 |
<h4> |
|
|
277 |
Attribute Examples:</h4> |
|
|
278 |
</div> |
|
|
279 |
<div class="bd"> |
|
|
280 |
<ul> |
|
|
281 |
<li><a href='../attribute/attribute-basic.html'>Basic Configuration</a></li><li><a href='../attribute/attribute-event.html'>Change Events</a></li><li><a href='../attribute/attribute-getset.html'>Getters, Setters and Validators</a></li><li class='selected'><a href='../attribute/attribute-rw.html'>readOnly and writeOnce Configuration</a></li> </ul> |
|
|
282 |
</div> |
|
|
283 |
</div> |
|
|
284 |
|
|
|
285 |
<div class="mod box4"> |
|
|
286 |
<div class="hd"> |
|
|
287 |
<h4>More Attribute Resources:</h4> |
|
|
288 |
</div> |
|
|
289 |
<div class="bd"> |
|
|
290 |
<ul> |
|
|
291 |
<!-- <li><a href="http://developer.yahoo.com/yui/attribute/">User's Guide</a> (external)</li> --> |
|
|
292 |
<li><a href="../../api/module_attribute.html">API Documentation</a></li> |
|
|
293 |
</ul> |
|
|
294 |
</div> |
|
|
295 |
</div> |
|
|
296 |
</div> |
|
|
297 |
</div> |
|
|
298 |
|
|
|
299 |
</div> |
|
|
300 |
</div> |
|
|
301 |
|
|
|
302 |
|
|
|
303 |
<div class="yui-b toc3" id="tocWrapper"> |
|
|
304 |
<!-- TABLE OF CONTENTS --> |
|
|
305 |
<div id="toc"> |
|
|
306 |
|
|
|
307 |
<ul> |
|
|
308 |
<li class="sect first">YUI 3.x Project</li><li class="item"><a title="The Yahoo! User Interface (YUI) Library, 3.x Branch, " href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site (external)</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="The YUI Library can be downloaded from SourceForge" href="http://sourceforge.net/projects/yui/">YUI 3 on Sourceforge (external)</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/3/license.html">YUI License (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="item"><a title="YUI (Global Prerequisite) - Functional Examples" href="../../examples/yui/index.html">YUI (Global Prerequisite)</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</a></li><li class="item"><a title="Widget - Functional Examples" href="../../examples/widget/index.html">Widget</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="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</a></li><li class="item"><a title="DataSource - Functional Examples" href="../../examples/datasource/index.html">DataSource</a></li><li class="item"><a title="DataType - Functional Examples" href="../../examples/datatype/index.html">DataType</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="History - Functional Examples" href="../../examples/history/index.html">History</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="Queue - Functional Examples" href="../../examples/queue/index.html">Queue</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</a></li><li class="item"><a title="Slider - Functional Examples" href="../../examples/slider/index.html">Slider</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</a></li><li class="item"><a title="MenuNav Node Plugin - Functional Examples" href="../../examples/node-menunav/index.html">MenuNav Node Plugin</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</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">The YUI Community</li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="item"><a title="The Yahoo! Group YDN-JavaScript hosts the YUI community forum" href="http://tech.groups.yahoo.com/group/ydn-javascript/">YUI Forum (external)</a></li><li class="item"><a title="The Yahoo! Group yui3 is dedicated to the 3.x branch of the Yahoo! User Interface (YUI) Library." href="http://tech.groups.yahoo.com/group/yui3/">YUI 3 Forum (external)</a></li><li class="item"><a title="YUI is used by Yahoo! and by hundreds of other sites, including many you know and love." href="/yui/poweredby/">YUI Sightings (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><li class="sect">YUI Articles on the YUI Website</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="Reporting Bugs and Making Feature Requests for YUI Components" href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests (external)</a></li><li class="item"><a title="Serve YUI source files from Yahoo! -- free, fast, and simple" href="http://developer.yahoo.com/yui/3/articles/hosting/">Serving YUI Files from Yahoo! (external)</a></li></ul> |
|
|
309 |
</div> |
|
|
310 |
</div> |
|
|
311 |
</div><!--closes bd--> |
|
|
312 |
|
|
|
313 |
<div id="ft"> |
|
|
314 |
<p class="first">Copyright © 2009 Yahoo! Inc. All rights reserved.</p> |
|
|
315 |
<p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> - |
|
|
316 |
<a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> - |
|
|
317 |
<a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> - |
|
|
318 |
<a href="http://careers.yahoo.com/">Job Openings</a></p> |
|
|
319 |
</div> |
|
|
320 |
</div> |
|
|
321 |
<script src="../../assets/dpSyntaxHighlighter.js"></script> |
|
|
322 |
<script language="javascript"> |
|
|
323 |
dp.SyntaxHighlighter.HighlightAll('code'); |
|
|
324 |
</script> |
|
|
325 |
</body> |
|
|
326 |
</html> |