1
YUI.add('gallery-event-binder', function(Y) {
4
* <p>The Event Binder satisfies a very specific need. Binding user actions until a
5
* particular YUI instance become ready, and the listeners defined before flushing those
6
* events through a queue. This will help to catch some early user interactions due
7
* the ondemand nature of YUI 3.
9
* <p>To use the Event Binder Module, you have to leverage YUI_config object first. More information
10
* about this object visit this page: http://developer.yahoo.com/yui/3/api/config.html<p>
11
* <p>There is a member of this global object that you have to set up, the member is called: "eventbinder". To get
12
* more information about this object, visit this page: http://yuilibrary.com/gallery/show/event-binder</p>
16
* <script type="text/javascript"> <br>
18
* // Call the "use" method, passing in "gallery-event-binder". Then you can<br>
19
* // call Y.EventBinder.flush('click'); to flush all the events that might had happened<br>
20
* // before your listeners were defined. <br>
22
* YUI().use("gallery-event-binder", "event", function(Y) { <br>
24
* Y.on('click', function(e) {<br>
25
* // do your stuff here...<br>
28
* Y.EventBinder.flush('click');<br>
34
* </script> <br>
38
* <p>The Event Binder has a single method called "flush". This method accept one argument to
39
* identify what type of event should be flushed. The argument can be:</p>
53
* <p>Keep in mind that before flushing any of these events, you have to add them to the
54
* monitoring system through the configuration object (YUI_config.eventbinder), otherwise
55
* YUI will be unable to listen for any early user interaction.</p>
58
* @module gallery-event-binder
61
function _modulesReady (e, modules, handler) {
62
var args = Y.Array(modules);
64
// stopping the module inmidiately
67
// adding the loading class
68
e.target.addClass('yui3-waiting');
70
args.push(function() {
71
// once the modules gets ready, let's remove the original binder
73
// removing the loading class
74
e.target.removeClass('yui3-waiting');
75
// let's simulate the new event based on the original facade
76
Y.Event.simulate(e.target._node, e.type, e);
79
Y.use.apply (Y, args);
85
* Filter all the events in the queue by type, and simulate those that match.
87
* @param type {string} The type of event to flush
89
flush: function (type) {
90
var config = Y.config.eventbinder || {};
92
config.q = config.q || [];
93
type = type || 'click';
96
// once you call flush, the original listener should be removed
97
Y.Event.detach(type, config.fn, Y.config.doc);
99
// filtering all the events in the queue by type
100
Y.each(config.q, function(o) {
102
if (type == o.type) {
103
// removing the loading class
104
Y.one(o.target).removeClass('yui3-waiting');
105
// let's simulate the new event based on the backup object described by "e" in the configuration
106
Y.Event.simulate(o.target, type, o);
112
* Adds an event listener. This method is an wrap for Y.on, and instead of supporting
113
* a regular callback, it loads a set of modules and simulate the same event once those
114
* modules become available.
116
* @param type {string} The type of event to append
117
* @param modules {string|array} a module or a list of modules that should be loaded when this event happens
118
* @param el {String|HTMLElement|Array|NodeList} An id, an element reference, or a collection of ids and/or elements to assign the listener to.
119
* @return {EventHandle} the detach handle
121
on: function (type, modules, el) {
122
// setting the event listener
123
var handler = Y.on (type, function(e) {
124
return _modulesReady(e, modules, handler);
128
* Adds an event listener. This method is an wrap for Y.on, and instead of supporting
129
* a regular callback, it loads a set of modules and simulate the same event once those
130
* modules become available.
132
* @param type {string} the event type to delegate
133
* @param modules {string|array} a module or a list of modules that should be loaded when this event happens
134
* @param el {String|HTMLElement|Array|NodeList} An id, an element reference, or a collection of ids and/or elements representing the delegation container.
135
* @param spec {string} a selector that must match the target of the event.
136
* @return {EventHandle} the detach handle
138
delegate: function (type, modules, el, spec) {
139
// setting the delegate listener
140
var handler = Y.delegate (type, function(e) {
141
return _modulesReady(e, modules, handler);
147
}, 'gallery-2010.06.23-18-37', {"requires": ["event-simulate", "event-base", "event-delegate"]});