Created
March 8, 2013 10:45
-
-
Save timlind/5115649 to your computer and use it in GitHub Desktop.
A jquery plugin to watch for and track events so that they become phases, which allows listeners to attach after the event occurs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* A jquery plugin to watch for and track events so that they become phases, which allows listeners to attach after the event occurs. */ | |
| $.prototype.capturePhase = function(eventName) { | |
| console.log("Watching for transition to phase " + eventName); | |
| var self = this; | |
| if (!this.__phaseCapturers__) this.__phaseCapturers__ = {}; | |
| if (!this.__phaseCapturers__[eventName]) { | |
| this.__phaseCapturers__[eventName] = true; | |
| this.on(eventName, function() { | |
| console.log("Captured transition to phase " + eventName); | |
| self.__phase__ = eventName; | |
| }) | |
| } | |
| return this; | |
| } | |
| $.prototype.onPhase = function(eventName, handler) { | |
| this.capturePhase(eventName); | |
| this.on(eventName, handler); | |
| if (this.__phase__ === eventName){ | |
| this.trigger(eventName); | |
| } | |
| return this; | |
| } | |
| // Example: | |
| // start capturing before the event triggers, i.e this is registering an event as a phase. | |
| $(window.applicationCache).capturePhase('noupdate'); | |
| // the event occurs which causes a transition into the "phase", handled by capturePhase. | |
| $(window.applicationCache).trigger('noupdate'); | |
| // and listeners that attach after the transition event will still be executed when they attach, | |
| // even though it happened in the past, and they will listen for future changes into this phase as well. | |
| $(window.applicationCache).onPhase('noupdate', function() { | |
| console.log("I'm sort of seeing into the past or something...at the same time as watching out for the future."); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I never got around to using this, it's something I've been wanted to write, but turns out I didn't need it for the situation that I thought I did.