Should an event listener be called if it is attached after the event has already been emitted? What if the event will only be emitted once?
The first example that comes to mind is the ready
event in jQuery. The following snippet, when evaluated after the page has loaded, will still call the callback:
$(document).ready(function () {
console.log("Works.");
});
The alternative to this behaviour could be a flag that is set when the page is loaded, forcing the consumer of the API to check to see if the event has already happened and act accordingly:
if ($.pageHasLoaded) {
console.log("Works.");
} else {
$(document).ready(function () {
console.log("Works.");
});
}
While the above example is in the context of a web page loading where anything and everything (usually) needs to happen after the page has completely loaded, the same arguments could be made for any single component in an application, which has singleton events (load
, start
, end
, etc).
Aucun commentaire:
Enregistrer un commentaire