I want to do something like this:
$("#container").otherBind("foo-content-loaded", function() {alert("immediate or later!");});
“foo-content-loaded” is a custom event I’m firing only once ever. otherBind is similar to $.ready(), such that the handler fires right away if the event has already been triggered or else it acts like a regular event.
how would otherBind() be defined? Incidently, what’s the cool CS name for this type of binding?
Instead of firing a custom event, use a Deferred:
Elsewhere, you can add as many
done()handlers as you like and have them called even if you add them after-the-fact:If you’re loading your content via
$.ajax, you should note that$.ajaxcalls already return a Promise interface (a restricted interface for Deferreds) to which you can attachdone()handlers.