event-ready.js revision 5ece432c4f3880bc9b74210154e12636755ed86b
569c3090f5818228805d517e135aa3799732292aRyan Grove
569c3090f5818228805d517e135aa3799732292aRyan Grove/**
569c3090f5818228805d517e135aa3799732292aRyan Grove * Custom event engine, DOM event listener abstraction layer, synthetic DOM
569c3090f5818228805d517e135aa3799732292aRyan Grove * events.
569c3090f5818228805d517e135aa3799732292aRyan Grove * @module event
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove */
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grovevar GLOBAL_ENV = YUI.Env;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove// if (Y !== YUI) {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove // return;
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove// }
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan GroveY.mix(Y.Env.eventAdaptors, {
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove
d5718409d7c1c4cbbd2be4605305c045a68a9136Ryan Grove /**
569c3090f5818228805d517e135aa3799732292aRyan Grove * Executes the supplied callback when the DOM is first usable. This
569c3090f5818228805d517e135aa3799732292aRyan Grove * will execute immediately if called after the DOMReady event has
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * fired. @todo the DOMContentReady event does not fire when the
569c3090f5818228805d517e135aa3799732292aRyan Grove * script is dynamically injected into the page. This means the
569c3090f5818228805d517e135aa3799732292aRyan Grove * DOMReady custom event will never fire in FireFox or Opera when the
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove * library is injected. It _will_ fire in Safari, and the IE
569c3090f5818228805d517e135aa3799732292aRyan Grove * implementation would allow for us to fire it if the defered script
569c3090f5818228805d517e135aa3799732292aRyan Grove * is not available. We want this to behave the same in all browsers.
569c3090f5818228805d517e135aa3799732292aRyan Grove * Is there a way to identify when the script has been injected
569c3090f5818228805d517e135aa3799732292aRyan Grove * instead of included inline? Is there a way to know whether the
569c3090f5818228805d517e135aa3799732292aRyan Grove * window onload event has fired without having had a listener attached
569c3090f5818228805d517e135aa3799732292aRyan Grove * to it when it did so?
84ae7c9d0c9d7a559d93a52393255678b6ac4e55Ryan Grove *
* <p>The callback is a Event.Custom, so the signature is:</p>
* <p>type &lt;string&gt;, args &lt;array&gt;, customobject &lt;object&gt;</p>
* <p>For DOMReady events, there are no fire argments, so the
* signature is:</p>
* <p>"DOMReady", [], obj</p>
*
*
* @event domready
* @for YUI
*
* @param {function} fn what to execute when the element is found.
* @optional context execution context
* @optional args 1..n arguments to send to the listener
*
*/
domready: {
},
/**
* Use domready event instead. @see domready
* @event event:ready
* @for YUI
* @deprecated use 'domready' instead
*/
'event:ready': {
on: function() {
arguments[0] = 'domready';
return Y.subscribe.apply(Y, arguments);
},
detach: function() {
arguments[0] = 'domready';
return Y.unsubscribe.apply(Y, arguments);
}
}
});
Y.publish('domready', {
fireOnce: true
});
var yready = function() {
Y.fire('domready');
};
if (GLOBAL_ENV.DOMReady) {
// Y.log('DOMReady already fired', 'info', 'event');
yready();
} else {
// Y.log('setting up before listener', 'info', 'event');
Y.before(yready, GLOBAL_ENV, "_ready");
}