1,721 articles and 13,139 comments as of Friday, October 8th, 2010

Thursday, April 9, 2009

JQuery for Everyone: Live Events vs. AOP

Live Events, new in jQuery 1.3, give us another tool when dealing with the SP user interface.

Clicking, moving the mouse, and pressing keys all trigger events in the web browser. In JavaScript, we try to capture these events, using event listeners that trigger interactions to control the user experience. In my previous examples, like loadTip-AOP and Preview Pane, I used event helpers to bind listeners to individual DOM elements:

$("a[href*='DispForm.aspx']").mouseenter(function(event){console.log(event);});

Try this: Put jQuery on your page. Run the code above in your Firebug console then move your mouse over a linked Item. On the left side, click the new event object. What type of information does an event hold?

When we use this technique, ExpGroupRenderData, our old nemesis, forces us to bind new events to the new DOM elements (hopefully without binding additional events to existing ones). I used two different ways to trigger this “rebinding” process: rewrite ExpGroupRenderData to include the binding function and AOP to create a sort of callback function.

How Live Events Are Better

As the number of elements on the page increases, the performance of this technique degrades. Event delegation, using live events, attaches the listener to a “container” element so the rebinding process is no longer needed. Additionally, since only the containers need listeners, the initial binding process performs better.

Trade-offs

As of this writing, jQuery 1.3 does not support live events for blur, focus, mouseenter, mouseleave, change, and submit. With a traditional .bind() approach, we can easily manage our 1:1 ratio of listeners to DOM elements. Using event delegation, we must take special care to avoid firing multiple, unwanted events.

Click “Read more…” if you want to see the hands-on experiment.

Hands on

Working with Live Events forces us to work from “most specific” to “most general” with our selectors. For example, open a page with more than one web part pointing to list Items (Tasks, Calendar, etc.). Place jQuery on the page (either a CEWP or the jQueryify bookmarklet) and run the following code in the Firebug console:

$("a[href*='DispForm.aspx']").mouseenter(function(event){
	console.log("1:"+event.target);
});
$("div#WebPartWPQ1 a[href*='DispForm.aspx']").mouseenter(function(event){
	console.log("2:"+event.target);
});

When you mouse over your links you will see either one or two console messages (depending on whether or not the target was inside web part 1). Let’s suppose, you only want one of these to fire. Refresh the page, add the .stopImmediatePropagation() method to our event and try again.

[/sourcecode]
$("a[href*='DispForm.aspx']").mouseenter(function(event){
	console.log("1:"+event.target);
	event.stopImmediatePropagation();
});
$("div#WebPartWPQ1 a[href*='DispForm.aspx']").mouseenter(function(event){
	console.log("2:"+event.target);
});
[sourcecode lang="xml"]

What happened? Well, we’re only seeing one console message but we never see the second function fire, even for links in web part 1. Because our selectors overlap, to achieve the desired results, we have to reverse the order so the browser processes our most specific selector first and stops propagation (the effect of bubbling the event up through the DOM).

[/sourcecode]
$("div#WebPartWPQ1 a[href*='DispForm.aspx']").mouseenter(function(event){
	console.log("2:"+event.target);
	event.stopImmediatePropagation();
});
$("a[href*='DispForm.aspx']").mouseenter(function(event){
	console.log("1:"+event.target);
});
[sourcecode lang="xml"]

Now we get message 2 for our web part 1 links and message 1 for all others.

Conclusion

Live events represent a great option for managing new UI interactions in SharePoint, especially when working with a changing DOM. However, the trade-offs require us to plan and test more to understand where our selectors overlap.

Paul Grenier

View all entries in this series: PaulGrenier-JQuery for Everyone»
Entries in this series:
  1. JQuery for Everyone: Accordion Left Nav
  2. JQuery for Everyone: Print (Any) Web Part
  3. JQuery for Everyone: HTML Calculated Column
  4. JQuery for Everyone: Dressing-up Links Pt1
  5. JQuery for Everyone: Dressing-up Links Pt2
  6. JQuery for Everyone: Dressing-up Links Pt3
  7. JQuery for Everyone: Cleaning Windows Pt1
  8. JQuery for Everyone: Cleaning Windows Pt2
  9. JQuery for Everyone: Fixing the Gantt View
  10. JQuery for Everyone: Dynamically Sizing Excel Web Parts
  11. JQuery for Everyone: Manually Resizing Web Parts
  12. JQuery for Everyone: Total Calculated Columns
  13. JQuery for Everyone: Total of Time Differences
  14. JQuery for Everyone: Fixing Configured Web Part Height
  15. JQuery for Everyone: Expand/Collapse All Groups
  16. JQuery for Everyone: Preview Pane for Multiple Lists
  17. JQuery for Everyone: Preview Pane for Calendar View
  18. JQuery for Everyone: Degrading Dynamic Script Loader
  19. JQuery for Everyone: Force Checkout
  20. JQuery for Everyone: Replacing [Today]
  21. JQuery for Everyone: Whether They Want It Or Not
  22. JQuery for Everyone: Linking the Attachment Icon
  23. JQuery for Everyone: Aspect-Oriented Programming with jQuery
  24. JQuery for Everyone: AOP in Action - loadTip Gone Wild
  25. JQuery for Everyone: Wiki Outbound Links
  26. JQuery for Everyone: Collapse Text in List View
  27. JQuery for Everyone: AOP in Action - Clone List Header
  28. JQuery for Everyone: $.grep and calcHTML Revisited
  29. JQuery for Everyone: Evolution of the Preview
  30. JQuery for Everyone: Create a Client-Side Object Model
  31. JQuery for Everyone: Print (Any) Web Part(s) Plugin
  32. JQuery for Everyone: Minimal AOP and Elegant Modularity
  33. JQuery for Everyone: Cookies and Plugins
  34. JQuery for Everyone: Live Events vs. AOP
  35. JQuery for Everyone: Live Preview Pane
  36. JQuery for Everyone: Pre-populate Form Fields
  37. JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
  38. Use Firebug in IE
  39. JQuery for Everyone: Extending OWS API for Calculated Columns
  40. JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
  41. JQuery for Everyone: Email a List of People with OWS
  42. JQuery for Everyone: Faster than Document.Ready
  43. jQuery for Everyone: Collapse or Prepopulate Form Fields
  44. jQuery for Everyone: Hourly Summary Web Part
  45. jQuery for Everyone: "Read More..." On a Blog Site
  46. jQuery for Everyone: Slick Speed Test
  47. jQuery for Everyone: The SharePoint Game Changer
  48. JQuery For Everyone: Live LoadTip
 

Please Join the Discussion

One Response to “JQuery for Everyone: Live Events vs. AOP”

Trackbacks

Check out what others are saying about this post...
  1. [...] JQuery for Everyone: Live Events vs. AOP [...]




Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!