|
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>Using Custom 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 |
#fire {margin:1em;} |
|
|
27 |
#log { |
|
|
28 |
border:1px dotted #999999; background-color:#FFFFFF; |
|
|
29 |
} |
|
|
30 |
#log li {padding:5px;} |
|
|
31 |
#log li highlight {color:#993300;} |
|
|
32 |
</style> |
|
|
33 |
<!--end custom header content for this example--> |
|
|
34 |
|
|
|
35 |
</head> |
|
|
36 |
|
|
|
37 |
<body class=" yui-skin-sam"> |
|
|
38 |
|
|
|
39 |
<h1>Using Custom Events</h1> |
|
|
40 |
|
|
|
41 |
<div class="exampleIntro"> |
|
|
42 |
<p>The Custom Event framework is one of the principle communication mechanisms |
|
|
43 |
in YUI. An object can be augmented with <code>EventTarget</code>, enabling it |
|
|
44 |
to be both a host and a target for Custom Events. Custom Events fire from |
|
|
45 |
their host and optionally bubble up to one or more targets. This allows you to |
|
|
46 |
make the interesting moments of your applications broadly available within a |
|
|
47 |
module, within a set of modules, or throughout a complex interface populated |
|
|
48 |
with rich elements.</p> |
|
|
49 |
|
|
|
50 |
<p>In this example, a simple Custom Event is illustrated: |
|
|
51 |
<code>testEvent</code>. This Custom Event is hosted on a Publisher object and |
|
|
52 |
bubbles up to a BubbleTarget object.</p> |
|
|
53 |
|
|
|
54 |
<p><img src="assets/ce-example.gif" alt="An |
|
|
55 |
illustration of the relationship between the Custom Event, its host, and its |
|
|
56 |
Bubble Target."></p> |
|
|
57 |
|
|
|
58 |
<p>Custom Events, like DOM events, can be stopped |
|
|
59 |
(<code>stopPropagation</code>) and their default behavior can be suppressed |
|
|
60 |
(<code>preventDefault</code>).</p> |
|
|
61 |
|
|
|
62 |
</div> |
|
|
63 |
|
|
|
64 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
65 |
|
|
|
66 |
<button id="fire" value="Fire">Fire publisher:testEvent</button> <br /> |
|
|
67 |
<input type="checkbox" id="stopPropagation"> <label for="stopPropagation">Stop Propagation (testEvent won't bubble to the BubbleTarget.)</label><br /> |
|
|
68 |
<input type="checkbox" id="preventDefault"> <label for="preventDefault">Prevent Default (testEvent's defaultFn won't fire.)</label> |
|
|
69 |
|
|
|
70 |
<ol id="log"> |
|
|
71 |
<li>Custom Event log messages will appear here.</li> |
|
|
72 |
</ol> |
|
|
73 |
|
|
|
74 |
<script language="javascript"> |
|
|
75 |
|
|
|
76 |
//Create a YUI instance: |
|
|
77 |
YUI({base:"../../build/", timeout: 10000}).use("node", "event-custom-complex", |
|
|
78 |
|
|
|
79 |
function(Y) { |
|
|
80 |
|
|
|
81 |
//Shortcut for our logging div: |
|
|
82 |
var logger = Y.one("#log"); |
|
|
83 |
|
|
|
84 |
//Our BubbleTarget object is an object to which our Custom Event |
|
|
85 |
//will be allowed to bubble. It needs itself to be an EventTarget, |
|
|
86 |
//so we'll use augment to make it an EventTarget: |
|
|
87 |
var BubbleTarget = function() { |
|
|
88 |
Y.log("Host constructor executed.", "info", "example"); |
|
|
89 |
} |
|
|
90 |
//Augment BubbleTarget to make it an EventTarget: |
|
|
91 |
Y.augment(BubbleTarget, Y.EventTarget); |
|
|
92 |
|
|
|
93 |
//Create an instance of BubbleTarget: |
|
|
94 |
var bubbleTarget = new BubbleTarget(); |
|
|
95 |
|
|
|
96 |
//Now we'll subscribe to the "publisher:testEvent" -- note |
|
|
97 |
//that we can do this even before this event is published: |
|
|
98 |
bubbleTarget.subscribe("publisher:testEvent", function(e) { |
|
|
99 |
Y.log("publisher:testEvent fired on the BubbleTarget object.", "info", "example"); |
|
|
100 |
}); |
|
|
101 |
|
|
|
102 |
//Now we'll create the constructor for the Publisher, which |
|
|
103 |
//is the direct host of our Custom Event. It will also be an |
|
|
104 |
//EventTarget, so we'll extend it as well: |
|
|
105 |
var Publisher = function(bubbleTarget) { |
|
|
106 |
|
|
|
107 |
//We'll specify now that Custom Events hosted by Publisher |
|
|
108 |
//should bubble to the bubbleTarget instance passed into the |
|
|
109 |
//the Publisher's constructor: |
|
|
110 |
this.addTarget(bubbleTarget); |
|
|
111 |
|
|
|
112 |
//Here we publish the Custom Event. Note that it's not |
|
|
113 |
//necessary to publish the event at all if you don't have |
|
|
114 |
//options you wish to configure -- firing the event or |
|
|
115 |
//subscribing to it will create it on the fly if necessary: |
|
|
116 |
this.publish("publisher:testEvent", |
|
|
117 |
{ |
|
|
118 |
emitFacade: true, |
|
|
119 |
//the defaultFn is what you want to have happen |
|
|
120 |
//by default when no subscriber calls preventDefault: |
|
|
121 |
defaultFn: function() { |
|
|
122 |
Y.log("defaultFn: publisher:testEvent's defaultFn executed.", "info", "example"); |
|
|
123 |
}, |
|
|
124 |
//You can prevent the default function from firing by |
|
|
125 |
//calling preventDefault from a listener (if the Custom |
|
|
126 |
//Event's preventable option is set to true, as it is by |
|
|
127 |
//default). If the default is prevented, the preventedFn |
|
|
128 |
//is called, allowing you to respond if necessary. |
|
|
129 |
preventedFn: function() { |
|
|
130 |
Y.log("preventedFn: A subscriber to publisher:testEvent called preventDefault().", "info", "example"); |
|
|
131 |
}, |
|
|
132 |
//The stoppedFn is called if a subscriber calls stopPropagation or |
|
|
133 |
//stopImmediatePropagation: |
|
|
134 |
stoppedFn: function() { |
|
|
135 |
Y.log("stoppedFn: A subscriber to publisher:testEvent called stopPropagation().", "info", "example"); |
|
|
136 |
} |
|
|
137 |
} |
|
|
138 |
); |
|
|
139 |
Y.log("Publisher constructor executed."); |
|
|
140 |
} |
|
|
141 |
//Augment Publisher to make it an EventTarget: |
|
|
142 |
Y.augment(Publisher, Y.EventTarget); |
|
|
143 |
|
|
|
144 |
//Create a Publisher instance: |
|
|
145 |
var p = new Publisher(bubbleTarget); |
|
|
146 |
|
|
|
147 |
//We've already subscribed to the event on the bubbleTarget, but |
|
|
148 |
//we can also subscribe to it here on the Publisher instance. |
|
|
149 |
//We'll see the event fire here before it bubbles up to the |
|
|
150 |
//bubbleTarget: |
|
|
151 |
p.on("publisher:testEvent", function(e) { |
|
|
152 |
Y.log("publisher:testEvent subscriber fired on the publisher object.", "info", "example"); |
|
|
153 |
if(Y.one("#stopPropagation").get("checked")) { |
|
|
154 |
//we will stopPropagation on the Custom Event, preventing |
|
|
155 |
//it from bubbling to the bubbleTarget: |
|
|
156 |
e.stopPropagation(); |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
if(Y.one("#preventDefault").get("checked")) { |
|
|
160 |
//we will preventDefault on the Custom Event, preventing |
|
|
161 |
//the testEvent's defaultFn from firing: |
|
|
162 |
e.preventDefault(); |
|
|
163 |
} |
|
|
164 |
}); |
|
|
165 |
|
|
|
166 |
//We can tie our testEvent to an interface gesture -- the click of a |
|
|
167 |
//button, for example. |
|
|
168 |
Y.on("click", function(e) { |
|
|
169 |
//clear out the logger: |
|
|
170 |
logger.set("innerHTML", ""); |
|
|
171 |
p.fire("publisher:testEvent"); |
|
|
172 |
}, "#fire"); |
|
|
173 |
|
|
|
174 |
//write out log messages to the page: |
|
|
175 |
Y.on("yui:log", function(e) { |
|
|
176 |
var s = logger.get("innerHTML"); |
|
|
177 |
logger.set("innerHTML", s + "<li>" + e.msg + "</li>"); |
|
|
178 |
}); |
|
|
179 |
|
|
|
180 |
}); |
|
|
181 |
</script> |
|
|
182 |
|
|
|
183 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
184 |
|
|
|
185 |
</body> |
|
|
186 |
</html> |