Created
June 25, 2011 05:53
-
-
Save jonathanconway/1046209 to your computer and use it in GitHub Desktop.
preBind() - Add an event binding *before* any pre-existing bindings. (An early version, may not work in all scenarios)
This file contains 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
$.fn.preBind = function(type, data, fn) { | |
var currentBindings = this.data('events')[type]; | |
var currentBindingsLastIndex = currentBindings.length - 1; | |
var newBindings = []; | |
// bind the event | |
this.bind(type, data, fn); | |
// move the new event to the top of the array | |
newBindings.push(currentBindings[currentBindingsLastIndex]); | |
$.each(currentBindings, function (index) { | |
if (index < currentBindingsLastIndex) | |
newBindings.push(this); | |
}); | |
this.data('events')[type] = newBindings; | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work with namespaced events like: