0

I was reading http://api.jquery.com/delegate/ and I learned that any .delegate() code can be written with an equivalent .on() code. The link provides this example:

For example, the following .delegate() code:

$( "table" ).delegate( "td", "click", function() {
    $( this ).toggleClass( "chosen" );
});

is equivalent to the following code written using .on():

$( "table" ).on( "click", "td", function() {
    $( this ).toggleClass( "chosen" );
});

I am using a legacy system with jQuery 1.3.1. It uses .livequery() extensively. I want to get rid of this Live Query plugin, but still continue to use jQuery 1.3.1 for a while. Please do not give me an answer recommending that I simply upgrade to the latest version of jQuery or use .on() instead, since .on() is not available in jQuery 1.3.1. For now, I must continue using jQuery 1.3.1, but replace all appearances of .livequery() with an equivalent using .delegate(). Is that possible? I am wondering if there are things that you do with .livequery(), that cannot be done with a .delegate() equivalent. Thank you.

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103
  • use [live](http://api.jquery.com/live/)? – Se0ng11 Sep 14 '17 at 02:21
  • @Se0ng11 Absolutely not. I need to use .delegate(), not .live(). Please read http://www.timacheson.com/Blog/2011/oct/jquery_live_vs_delegate. Also, .live() is already deprecated in modern versions of jQuery and my plan is to migrate to a newer version of jQuery later on, and .delegate() can survive a little longer than .live() as I keep moving to newer versions. .live() was removed in jQuery 1.9, whereas .delegate() survived a little longer and it was removed until jQuery 3.0. – Jaime Montoya Sep 14 '17 at 02:26
  • 1
    Should be possible. I think you need to try converting a few actual instances of `.livequery()` in your code in order to get a feel for the complexity of the conversion. – Roamer-1888 Sep 14 '17 at 03:08
  • 1
    The `delegate()` method was added in version 1.4.2, so no, you can't use it if you're restricted to 1.3.1. – nnnnnn Sep 14 '17 at 03:14
  • If livequery has been used to its fullest, you will need to use `.unbind()` and `.undelegate()` in addition to `.delegate()`. – Roamer-1888 Sep 14 '17 at 03:16
  • 1
    That said, nnnn's comment pours cold water on the whole enterprise. – Roamer-1888 Sep 14 '17 at 03:18
  • @nnnnnn I had not paid attention to the fact that the delegate() method was added in version 1.4.2. How can I get rid of Live Query and use a native jQuery equivalent that is available in jQuery 1.3.1? Is there a way to do that? – Jaime Montoya Sep 14 '17 at 03:21
  • @Roamer-1888 True, cold water was poured on the entire enterprise as you said. Basically what I want to do is to get rid of Live Query without upgrading jQuery yet, since I still need to use jQuery 1.3.1 to keep everything compatible with other legacy plugins that I have. Is there a way to get rid of the Live Query plugin and use native jQuery 1.3.1 equivalents instead? – Jaime Montoya Sep 14 '17 at 03:23
  • 1
    Sorry, I'm not familiar with the Live Query plugin. In a general sense if I needed delegate behavior in a pre-1.4.2 version of jQuery I'd just use `.live()`, or in your case I'd stick with the plugin for now, then later switch to `.delegate()` or `.on()` if you're eventually able to upgrade to jQuery 1.4.2+ or 1.7+. – nnnnnn Sep 14 '17 at 03:24
  • 1
    Yes, using POJS but you will drive yourself nuts getting your mind round the two different bubbling models. – Roamer-1888 Sep 14 '17 at 03:24
  • 1
    Realistically, I think you need to consider a different upgrade route, ie. upgrade jQuery before trying to purge livequery. – Roamer-1888 Sep 14 '17 at 03:35
  • @Roamer-1888 I followed your advice and thank you because following the route you suggested saved time. Now I have my site working correctly running jQuery 1.12.4. That was a great improvement compared to the old jQuery 1.3.1 that I was using. The .livequery() plugin is working correctly with jQuery 1.12.4. Only .live() is not working because it was deprecated in .jQuery 1.7 and removed in jQuery 1.9. Considering that the .livequery() plugin is working correctly on jQuery 1.12.4, do you think it is recommended to translate the .livequery() instances to .on() or just keep using livequery()? – Jaime Montoya Sep 22 '17 at 02:56
  • @nnnnnn I was able to upgrade to jQuery 1.12.4 and the .livequery() plugin is working correctly. Now I am trying to decide whether or not I should start translating every instance of .livequery() into its .on() equivalent, or just move on and keep using the .livequery() plugin. Of course, from now on I will not be writing new code using .livequery() because .on() can do what .livequery() does, but I am trying to decide if it makes sense to invest the time and effort to convert every instance of .livequery() into its .on() equivalent and test it. – Jaime Montoya Sep 22 '17 at 03:02
  • 1
    I wouldn't rewrite an existing, working app, I don't think the benefit would be worth the time involved. If you're doing other maintenance on the app and can switch over to `.on()` here and there as you go then I'd maybe do that. – nnnnnn Sep 22 '17 at 03:54
  • 1
    Mmm, decisions decisions. Depends largely on what your motivation was to remove livequery in the first place. If you still want to do so, then no question - use `.on()` (and `.off()`). But also consider @nnnnnn's idea to convert on an opportunity basis when maintaining for other reasons. To come to an informed decision, backup then convert one or two instances of `.livequery()` to gauge the difficulty/effort required. – Roamer-1888 Sep 22 '17 at 12:26
  • 1
    I rather expect that the conversion will be simple but testing will be time consuming unless you already use automated testing - Jasmine and the like. – Roamer-1888 Sep 22 '17 at 12:30
  • 1
    @Roamer-1888 I was able to do the upgrade successfully and kept the Live Query plugin because my motivation was upgrading jQuery, not getting rid of the Live Query plugin. Thank you. – Jaime Montoya Oct 12 '17 at 21:14
  • @nnnnnn Finally I completed the upgrade and I followed your advice. Everything is working correctly for me and of course from now on I will use .on(). Thank you! – Jaime Montoya Oct 12 '17 at 21:15

1 Answers1

1

I kept Live Query because my main objective was upgrading jQuery, not getting rid of Live Query.

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103