Created
June 17, 2010 09:21
-
-
Save remy/441895 to your computer and use it in GitHub Desktop.
Experiment to see if .live can be replaced - no it shouldn't be
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
/********* DON'T USE THIS **************/ | |
/** | |
* After thinking about it for a while, and as Kobi's comment says below | |
* this still has the wasteful selector which is one of the big reasons | |
* to use delegate in the first place - so please don't use this. | |
* - cheers, @rem | |
*/ | |
(function ($) { | |
$.root = $(document); // hat tip: Cory Hart | |
// keep safe | |
$.fn._live = $.fn.live; | |
$.fn._die = $.fn.die; | |
$.fn.live = function (type, fn) { | |
$.root.delegate(this.selector, type, fn); | |
return this; | |
}; | |
$.fn.die = function (type, fn) { | |
$.root.undelegate(this.selector, type, fn); | |
return this; | |
}; | |
})(jQuery); |
@kobi - yeah, I just realised this a moment ago, the wasteful select is still happening so there's no point in replicating this at all. Much better just to do:
$(document).delegate('div', 'click', fn);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It appears that the delegate method uses live (line 2411 http://code.jquery.com/jquery-1.4.2.js) so you enter an infinite loop using delegate in a redefined live method.