Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting livequery to live/delegate/?

Searching hasn't revealed a solution to my question so asking here. I've been using livequery() for a while now in combination with live(). Generally, I use live() for known events like click but using livequery for triggering on a specific selector and applying a plugin. For example...

$('#childDomains').livequery(function() {
  var element = $(this);
  element.jqGrid({
    // plugin initialization here
  });
});

I'm wondering if there is a way to do the same thing using live() / delegate() or some other cool jQuery feature I don't know about. I'm attempting to lighten my dependency load and I noticed that livequery hasn't been updated since 02-2010.

UPDATE: I guess I'm not being clear enough. I have a page that uses jqGrid and turns a TABLE element into a jqGrid Ajax data grid. There is no real click event for this to occur. The page that this table is on is loaded via ajax and anytime the DOM sees $("#childDomains") it should apply the jqGrid plugin to that element. Just like my livequery example is doing.

From my understanding thus far, I don't believe this is possible without livequery, but I wanted to ask to make sure.

like image 751
Gregg Avatar asked Oct 10 '22 16:10

Gregg


1 Answers

No, there are no clearly better ways to do that using jQuery; I would say livequery is the best method at the moment.

That said are other possible ways to do it. Here someone has rolled their own polling code to detect when an element is inserted into the DOM. You could adapt this, but as far as I can see livequery is already doing this for you.

Another way is to listen for DOMNodeInserted events as outlined here but this is deprecated in the W3C Level 3 Events Specification, due to poor performance. It is also not IE compatible.

This question has some good discussion on monitoring DOMNodeInserted events and an alternative of hooking into any methods that can change the DOM. (It is a little old, but from my research nothing has changed yet).

In conclusion, keep using livequery.

Also note that from jQuery 1.7 .live() (deprecated), and .delegate() (superseded) should be replaced with .on() (and .die() with .off()). The new methods do not introduce anything that can resolve your livequery dependency though.

like image 101
Sean Avatar answered Oct 13 '22 10:10

Sean