|
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>Attribute Change Events</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 .event { |
|
|
27 |
padding:2px 2px 2px 5px; |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
#example-out .event-props { |
|
|
31 |
font-family:courier; |
|
|
32 |
margin-top:2px; |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
#example-out .event-title { |
|
|
36 |
font-weight:bold; |
|
|
37 |
font-family:arial; |
|
|
38 |
color:#8dd5e7; |
|
|
39 |
margin-top:5px; |
|
|
40 |
margin-bottom:3px; |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
#example-out { |
|
|
44 |
margin:5px; |
|
|
45 |
border:1px solid #000; |
|
|
46 |
color:#ffffff; |
|
|
47 |
background-color:#004c6d; |
|
|
48 |
overflow:auto; |
|
|
49 |
height:20em; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
.attrs { |
|
|
53 |
border:1px solid #000; |
|
|
54 |
background-color:#cdcdcd; |
|
|
55 |
margin:5px; |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
.attrs .header { |
|
|
59 |
font-weight:bold; |
|
|
60 |
background-color:#aaa; |
|
|
61 |
padding:5px; |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
.attrs .body { |
|
|
65 |
padding:10px; |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
.attrs .footer { |
|
|
69 |
padding:5px; |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
.attrs label { |
|
|
73 |
display:block; |
|
|
74 |
float:left; |
|
|
75 |
clear:left; |
|
|
76 |
font-weight:bold; |
|
|
77 |
width:8em; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
.attrs #preventFoobar.hidden { |
|
|
81 |
display:none; |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
.attrs #preventFoobar { |
|
|
85 |
margin-left:5px; |
|
|
86 |
display:inline; |
|
|
87 |
float:none; |
|
|
88 |
clear:none; |
|
|
89 |
} |
|
|
90 |
</style> |
|
|
91 |
<!--end custom header content for this example--> |
|
|
92 |
|
|
|
93 |
</head> |
|
|
94 |
|
|
|
95 |
<body class=" yui-skin-sam"> |
|
|
96 |
|
|
|
97 |
<h1>Attribute Change Events</h1> |
|
|
98 |
|
|
|
99 |
<div class="exampleIntro"> |
|
|
100 |
Attribute change events are one of the key benefits of using attributes to maintain state for your objects, instead of regular object properties. This example shows how you can listen for attribute change events and work with the event payload they receive. |
|
|
101 |
|
|
|
102 |
</div> |
|
|
103 |
|
|
|
104 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
105 |
|
|
|
106 |
<div class="attrs"> |
|
|
107 |
<div class="header">Enter a new value and click the "Change Value" button:</div> |
|
|
108 |
<div class="body"> |
|
|
109 |
<p> |
|
|
110 |
<label for="attrSel">Attribute</label>: |
|
|
111 |
<select id="attrSel"> |
|
|
112 |
<option value="foo">foo</option> |
|
|
113 |
<option value="bar">bar</option> |
|
|
114 |
<option value="foobar">foobar</option> |
|
|
115 |
</select> |
|
|
116 |
<label id="preventFoobar" class="hidden"><input type="checkbox" checked="true"> Prevent change</label> |
|
|
117 |
</p> |
|
|
118 |
<p><label for="currentVal">Current Value</label>: <span id="currentVal"></span></p> |
|
|
119 |
<p><label for="newVal">New Value</label>: <input type="text" id="newVal" /></p> |
|
|
120 |
</div> |
|
|
121 |
<div class="footer"> |
|
|
122 |
<button type="button" id="changeValue">Change Value</button> |
|
|
123 |
</div> |
|
|
124 |
</div> |
|
|
125 |
|
|
|
126 |
<div id="example-out"></div> |
|
|
127 |
|
|
|
128 |
<script type="text/javascript"> |
|
|
129 |
// Get a new YUI instance |
|
|
130 |
YUI({base:"../../build/", timeout: 10000}).use("node", "attribute", function(Y) { |
|
|
131 |
|
|
|
132 |
// Setup a custom class with attribute support |
|
|
133 |
function MyClass(cfg) { |
|
|
134 |
|
|
|
135 |
// Setup attribute configuration |
|
|
136 |
var attrs = { |
|
|
137 |
"foo" : { |
|
|
138 |
value:5 |
|
|
139 |
}, |
|
|
140 |
|
|
|
141 |
"bar" : { |
|
|
142 |
value:"Hello World!" |
|
|
143 |
}, |
|
|
144 |
|
|
|
145 |
"foobar" : { |
|
|
146 |
value:true |
|
|
147 |
} |
|
|
148 |
}; |
|
|
149 |
|
|
|
150 |
this.addAttrs(attrs, cfg); |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
Y.augment(MyClass, Y.Attribute); |
|
|
154 |
|
|
|
155 |
var o1 = new MyClass(); |
|
|
156 |
|
|
|
157 |
function displayEvent(e, title) { |
|
|
158 |
var str = '<div class="event"><div class="event-title">' + title + '</div>'; |
|
|
159 |
|
|
|
160 |
if (e) { |
|
|
161 |
str += |
|
|
162 |
'<ul class="event-props"><li>e.attrName: ' |
|
|
163 |
+ e.attrName |
|
|
164 |
+ '</li><li>e.prevVal: ' |
|
|
165 |
+ e.prevVal |
|
|
166 |
+ '</li><li>e.newVal: ' |
|
|
167 |
+ e.newVal |
|
|
168 |
+ '</li></ul></div>'; |
|
|
169 |
} |
|
|
170 |
|
|
|
171 |
str += '</div>'; |
|
|
172 |
|
|
|
173 |
Y.one("#example-out").prepend(str); |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
// Start Example Form Handling |
|
|
177 |
var attrSel = Y.one("#attrSel"); |
|
|
178 |
var newValTxt = Y.one("#newVal"); |
|
|
179 |
var currentValSpan = Y.one("#currentVal"); |
|
|
180 |
var preventFoobarChk = Y.one("#preventFoobar input[type=checkbox]"); |
|
|
181 |
var preventFoobarLbl = Y.one("#preventFoobar"); |
|
|
182 |
|
|
|
183 |
var attrOpts = attrSel.get("options"); |
|
|
184 |
|
|
|
185 |
function updateVal() { |
|
|
186 |
var selIndex = attrSel.get("selectedIndex"); |
|
|
187 |
var attr = attrOpts.item(selIndex).get("value"); |
|
|
188 |
o1.set(attr, newValTxt.get("value")); |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
Y.on("click", updateVal, "#changeValue"); |
|
|
192 |
|
|
|
193 |
function populateCurrentValue() { |
|
|
194 |
var selIndex = attrSel.get("selectedIndex"); |
|
|
195 |
var attr = attrOpts.item(selIndex).get("value"); |
|
|
196 |
|
|
|
197 |
currentValSpan.set("innerHTML", o1.get(attr)); |
|
|
198 |
newValTxt.set("value", ""); |
|
|
199 |
|
|
|
200 |
if (attr === "foobar") { |
|
|
201 |
preventFoobarLbl.removeClass("hidden"); |
|
|
202 |
} else { |
|
|
203 |
preventFoobarLbl.addClass("hidden"); |
|
|
204 |
} |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
populateCurrentValue(); |
|
|
208 |
|
|
|
209 |
Y.on("change", populateCurrentValue, attrSel); |
|
|
210 |
// End Example Form Handling |
|
|
211 |
|
|
|
212 |
// Attribute Change Event Listners |
|
|
213 |
|
|
|
214 |
o1.after("fooChange", function(e) { |
|
|
215 |
displayEvent(e, "After fooChange"); |
|
|
216 |
currentValSpan.set("innerHTML", e.newVal); |
|
|
217 |
}); |
|
|
218 |
|
|
|
219 |
o1.after("barChange", function(e) { |
|
|
220 |
displayEvent(e, "After barChange"); |
|
|
221 |
currentValSpan.set("innerHTML", e.newVal); |
|
|
222 |
}); |
|
|
223 |
|
|
|
224 |
o1.on("foobarChange", function(e) { |
|
|
225 |
|
|
|
226 |
if (preventFoobarChk.get("checked")) { |
|
|
227 |
|
|
|
228 |
// Calling preventDefault, in an "on" listener |
|
|
229 |
// will prevent the attribute change from occuring |
|
|
230 |
// and the after listener being called. |
|
|
231 |
|
|
|
232 |
e.preventDefault(); |
|
|
233 |
displayEvent(null, "On foobarChange (prevented)"); |
|
|
234 |
} |
|
|
235 |
|
|
|
236 |
}); |
|
|
237 |
|
|
|
238 |
o1.after("foobarChange", function(e) { |
|
|
239 |
|
|
|
240 |
// This foobar after listener will not get called, |
|
|
241 |
// if we end up preventing default in the "on" |
|
|
242 |
// listener above. |
|
|
243 |
|
|
|
244 |
displayEvent(e, "After foobarChange"); |
|
|
245 |
currentValSpan.set("innerHTML", e.newVal); |
|
|
246 |
}); |
|
|
247 |
|
|
|
248 |
}); |
|
|
249 |
</script> |
|
|
250 |
|
|
|
251 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
252 |
|
|
|
253 |
</body> |
|
|
254 |
</html> |