|
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 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>Read-Only and Write-Once Attributes</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-top:5px; |
|
|
45 |
margin-bottom:20px; |
|
|
46 |
height:8em; |
|
|
47 |
padding:2px 2px 2px 5px; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
#writeInitial label, #writeAgain label, #writeInternally label { |
|
|
51 |
font-weight:900; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
#writeInitial .fooVal, #writeAgain .fooVal, #writeInternally .fooVal { |
|
|
55 |
width:9em; |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
#writeInitial .barVal, #writeAgain .barVal, #writeInternally .barVal { |
|
|
59 |
width:9em; |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
#writeInitial p, #writeAgain p, #writeInternally p { |
|
|
63 |
margin-top:0.2em; |
|
|
64 |
margin-bottom:0.2em; |
|
|
65 |
color:#004C6D; |
|
|
66 |
font-size:108%; |
|
|
67 |
} |
|
|
68 |
</style> |
|
|
69 |
|
|
|
70 |
<!--end custom header content for this example--> |
|
|
71 |
|
|
|
72 |
</head> |
|
|
73 |
|
|
|
74 |
<body class=" yui-skin-sam"> |
|
|
75 |
|
|
|
76 |
<h1>Read-Only and Write-Once Attributes</h1> |
|
|
77 |
|
|
|
78 |
<div class="exampleIntro"> |
|
|
79 |
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. |
|
|
80 |
|
|
|
81 |
</div> |
|
|
82 |
|
|
|
83 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
84 |
|
|
|
85 |
<div id="writeInitial"> |
|
|
86 |
<p>Construct o1, setting initial values for both foo and bar in the constructor: </p> |
|
|
87 |
<label>foo: <input type="text" value="Initial Foo" class="fooVal"></label> |
|
|
88 |
<label>bar: <input type="text" value="Initial Bar" class="barVal"></label> |
|
|
89 |
<button type="button" class="do">Initial Values</button> |
|
|
90 |
<div class="example-out"></div> |
|
|
91 |
</div> |
|
|
92 |
<div id="writeAgain"> |
|
|
93 |
<p>Try setting values again, after they've been set once: </p> |
|
|
94 |
<label>foo: <input type="text" value="Set Foo Again" class="fooVal"></label> |
|
|
95 |
<label>bar: <input type="text" value="Set Bar Again" class="barVal"></label> |
|
|
96 |
<button type="button" class="do">Set Again</button> |
|
|
97 |
<div class="example-out"></div> |
|
|
98 |
</div> |
|
|
99 |
<div id="writeInternally"> |
|
|
100 |
<p>Call a MyClass method, which sets foo internally (using _set): </p> |
|
|
101 |
<label>foo: <input type="text" value="Set Foo Internally" class="fooVal"></label> |
|
|
102 |
<button type="button" class="do">Set Internal</button> |
|
|
103 |
<div class="example-out"></div> |
|
|
104 |
</div> |
|
|
105 |
|
|
|
106 |
<script type="text/javascript"> |
|
|
107 |
YUI({base:"../../build/", timeout: 10000}).use("node", "attribute", function(Y) { |
|
|
108 |
|
|
|
109 |
// Setup a custom class with attribute support |
|
|
110 |
function MyClass(cfg) { |
|
|
111 |
|
|
|
112 |
var attrs = { |
|
|
113 |
"foo" : { |
|
|
114 |
value: "Default Foo", |
|
|
115 |
readOnly: true |
|
|
116 |
}, |
|
|
117 |
|
|
|
118 |
"bar" : { |
|
|
119 |
value: "Default Bar", |
|
|
120 |
writeOnce: true |
|
|
121 |
} |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
this.addAttrs(attrs, cfg); |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
MyClass.prototype.doSomething = function(val) { |
|
|
128 |
// ... Do something which requires |
|
|
129 |
// MyClass to change the value |
|
|
130 |
// of foo ... |
|
|
131 |
|
|
|
132 |
// Host code can reset value of |
|
|
133 |
// readOnly attributes interally, |
|
|
134 |
// by working with the private _set |
|
|
135 |
// property |
|
|
136 |
|
|
|
137 |
this._set("foo", val); |
|
|
138 |
}; |
|
|
139 |
|
|
|
140 |
Y.augment(MyClass, Y.Attribute); |
|
|
141 |
|
|
|
142 |
function displayValues(o, title, node) { |
|
|
143 |
var str = |
|
|
144 |
'<div class="myclass"><div class="myclass-title">' |
|
|
145 |
+ title + |
|
|
146 |
'</div><ul class="myclass-attrs"><li>foo (readOnly): ' |
|
|
147 |
+ o.get("foo") |
|
|
148 |
+ '</li><li>bar (writeOnce): ' |
|
|
149 |
+ o.get("bar") |
|
|
150 |
+ '</li></ul></div>'; |
|
|
151 |
|
|
|
152 |
Y.one(node).set("innerHTML", str); |
|
|
153 |
} |
|
|
154 |
|
|
|
155 |
var o1; |
|
|
156 |
|
|
|
157 |
Y.on("click", function() { |
|
|
158 |
|
|
|
159 |
var fooVal = Y.one("#writeInitial .fooVal").get("value"); |
|
|
160 |
var barVal = Y.one("#writeInitial .barVal").get("value"); |
|
|
161 |
|
|
|
162 |
o1 = new MyClass({ |
|
|
163 |
foo: fooVal, |
|
|
164 |
bar: barVal |
|
|
165 |
}); |
|
|
166 |
|
|
|
167 |
displayValues(o1, "Attempt to set initial values in constructor", "#writeInitial .example-out"); |
|
|
168 |
|
|
|
169 |
}, "#writeInitial .do"); |
|
|
170 |
|
|
|
171 |
Y.on("click", function() { |
|
|
172 |
|
|
|
173 |
if (o1) { |
|
|
174 |
var fooVal = Y.one("#writeAgain .fooVal").get("value"); |
|
|
175 |
var barVal = Y.one("#writeAgain .barVal").get("value"); |
|
|
176 |
|
|
|
177 |
// Attempt to reset values: |
|
|
178 |
o1.set("foo", fooVal); |
|
|
179 |
o1.set("bar", barVal); |
|
|
180 |
|
|
|
181 |
displayValues(o1, "Attempt to set values again, using set", "#writeAgain .example-out"); |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
}, "#writeAgain .do"); |
|
|
185 |
|
|
|
186 |
Y.on("click", function() { |
|
|
187 |
|
|
|
188 |
if (o1) { |
|
|
189 |
var val = Y.one("#writeInternally .fooVal").get("value"); |
|
|
190 |
// Call method, which sets foo internally... |
|
|
191 |
o1.doSomething(val); |
|
|
192 |
|
|
|
193 |
displayValues(o1, "Set the value of foo (readOnly) internally", "#writeInternally .example-out"); |
|
|
194 |
} |
|
|
195 |
|
|
|
196 |
}, "#writeInternally .do"); |
|
|
197 |
}); |
|
|
198 |
</script> |
|
|
199 |
|
|
|
200 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
201 |
|
|
|
202 |
</body> |
|
|
203 |
</html> |