Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate between delegated events and their host event #44

Open
DataTables opened this issue Feb 8, 2014 · 2 comments
Open

Differentiate between delegated events and their host event #44

DataTables opened this issue Feb 8, 2014 · 2 comments

Comments

@DataTables
Copy link
Collaborator

jQuery delegated events are shown a little confusingly in Visual Event at the moment.

What happens is that it shows the parent event (i.e. the one that jQuery actually attaches to the DOM) and the elements that would trigger the event handler - so, for example if you have a UL with 5 LI tags in it, and attach a delegate event handler to the LIs with the UI as the host, you would see six events in Visual Event.

While this is sort of correct, Visual Event could do a better job of explaining what is going on (currently it shows 'live' text - this should be updated for newer jQuery). It would also be good to update the counters in the bar at the bottom of the window so say how many actual event handlers there are and how many delegated events handlers on child elements - ideally with a way to enable and disable each view so you can filter the view yourself to what you want.

Other libraries probably also have something similar, but lets start with jQuery...

@brownieboy
Copy link

More than a little confusing, I'd say. It's totally misleading, in fact.

I looked at a web project today where Visual Event reported nearly 20,000 event handlers on a single page app! Of course, the real number of handlers was only a fraction of that. The high number was down to VE's seeming inability to distinguish between actual handlers and delegated ones.

Here's two example pages.
http://www.users.on.net/~mikeandgeminoz/event.test/non-delegated.html
http://www.users.on.net/~mikeandgeminoz/event.test/delegated.html

The both contain a single table with click event handers set on the rows via jQuery. In the first example, the event handlers are really set on each row, like so:

$('#myTable tbody tr').click(function () {
  alert("hey " + $(this).attr("data-payload"));
});

Visual Event reports 900+ events, which is correct: one for each row.

On the second page, however, the event handlers are delegated, like so:

$('#myTable tbody').delegate('tr', 'click', function () {
alert("hey " + $(this).attr("data-payload"));
});

What this means is that only single event handler is actually registered, at the tbody level. But the jQuery.delegate() function is still able to ascertain which TR row within the tbody you actually clicked on. (jQuery.on can work the same way.) Yet Visual Event is still reporting that same 900+ event handlers, and each table row is still highlighted as having its own event handler.

Sorry, but that's just plain wrong! There's only one event handler on the second page. This is confirmed by the Firefox HTML Inspector. (That's the one built into Firefox, and not Firebug.) The Firefox Inspector now shows a white on black "EV" box next to every HTML element that it deems to have an event handler. And this correctly shows an "EV" box next to every TR tag on my first page, but only a single "EV" box next to the tbody tag on my second page. A good deal more reliable that Visual Event is at the moment.

@DataTables
Copy link
Collaborator Author

Thanks for the feedback. I haven't had time to work on this yet, but it does need to be done when I get a chance to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant