Created
December 11, 2012 06:38
-
-
Save Iheartweb/4256376 to your computer and use it in GitHub Desktop.
Just in Time Execution for Lu
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
| /** | |
| * The code in this gist allows for just in time execution of Lu components. | |
| * JITE will be a fall-back in browsers that don't support MutationObservation | |
| * or MutationEvents or when a processor has not executed a component yet. | |
| */ | |
| function gatherDeferrals(components) { | |
| var deferrals = []; | |
| _.each(components, function (component) { | |
| if (component.status !== 'ready') { | |
| deferrals.push(component.deferral); | |
| if (component.status === 'mapped') { | |
| component.execute(); | |
| } | |
| } | |
| }); | |
| return deferrals; | |
| } | |
| /** | |
| * Gather related components and components that will receive a triggered | |
| * event and execute them. After they are executed, execute all handlers and | |
| * behaviors attached to the element used during instantiation for the given | |
| * event type. This is a proxy for jQuery's trigger method. | |
| * @method trigger | |
| * @public | |
| * @chainable | |
| */ | |
| trigger: function () { | |
| var self = this, | |
| //make an array of args passed in | |
| parameters = Array.prototype.slice.call(arguments), | |
| //gather the parents that have been mapped by Lu | |
| $parents = HELPERS.getParents.call(this.$element, Lu.cache), | |
| //a place to store deferrals | |
| deferrals = []; | |
| //prefix the events with "lu:" so that they don't collide with non lu events | |
| parameters[0] = prefix(parameters[0]); | |
| //gather the components that the event will bubble to | |
| _.each($parents, function (parent) { | |
| var components = HELPERS.getComponents.call($(parent)); | |
| //for each of those components if it is mapped (ready to be executed) | |
| //then add its deferral to the deferrals array and execute it | |
| deferals = deferrals.concat(gatherDeferrals(components)); | |
| }); | |
| //do the same as above but with related components. Relation is determined | |
| //by aria relationship attributes and configuration. | |
| deferals = deferrals.concat(gatherDeferrals(this.getRelated())); | |
| //Wait for it! | |
| $.when.apply($, deferrals).then(function() { | |
| //trigger | |
| self.$element.trigger.apply(self.$element, parameters); | |
| }); | |
| //notify is also a deferred | |
| this.notify.apply(this, parameters); | |
| //make it chainable | |
| return this; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The downside to this is that is it potentially a lot to do every time an event is triggered. However once a branch is fully executed this should be pretty fast.