Skip to content

Instantly share code, notes, and snippets.

@jonathanconway
Created June 25, 2011 05:53
Show Gist options
  • Save jonathanconway/1046209 to your computer and use it in GitHub Desktop.
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)
$.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;
};
@nevcos
Copy link

nevcos commented Dec 19, 2012

Doesn't work with namespaced events like:

$element.preBind("click.namespace", function() {});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment