1,804 articles and 15,136 comments as of Sunday, May 15th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Monday, February 16, 2009

JQuery for Everyone: Aspect-Oriented Programming with jQuery

The jQuery community grows daily with new converts and new projects. I try to stay on top of it, but with no programming background I also find myself back-peddling through wiki articles when I come across foreign concepts like Aspect-Oriented Programing (AOP).

The more I read about AOP and what the jquery-AOP project aims to do, the more excited I got about adding it to my SharePoint toolbox. However, after my first test case, I started wondering if I would ever use it.

What it does:
The jquery-AOP project used jQuery to create functions that can treat object methods and global functions as aspects. That means, that you can run other code before, after, or around them.

For example, let’s say you want a pop-up message box to appear before someone uses the document check-out feature. You have a couple of choices: you can rewrite the check-out function(s) to include your message, rewrite the links/code that trigger the check-out to include your code first, or you can treat the check-out function as an aspect.

jQuery.aop.before( {target: window, method: 'SomeSharePointFunction'},
  function() {
    alert('About to execute SomeSharePointFunction');
  }
);

By treating the OOB function as an aspect, we accomplish a few goals:

  1. We implement our change “globally” with minimal change to the rendered page.
  2. We keep OOB code in tact so future updates don’t trash our stuff and we can still use the OOB code as intended.
  3. We implement our change seamlessly avoiding pause routines and other timing hacks.

What it Doesn’t Do (Easily):
My first choice for this pattern, the infamous ExpGroupRenderData(htmlToRender, groupName, isLoaded) function. As an aspect, we could fire our code before, after or around it. That’s brilliant because I’m always rewriting that function for my work with dynamically loading content. SharePoint uses that function to render HTML it loads on demand due to events like expanding groups.

However, my code running before or after ExpGroupRenderData runs completely independently and without sharing of variables or parameters. The event that triggers ExpGroupRenderData does so passing important information: htmlToRender, groupName, and isLoaded. If my code needs that data, I have no easy way to get it.

Do we need that data?
Well, in cases where we don’t care if a process runs over an element multiple times (like HTML calculated columns), we can use it. However, if we’re binding events (like an onmouseover to trigger a preview), the event will get bound multiple times. This could cause problems as we trigger the event and our code fires off multiple times (if it’s a GET request, we’re about to make our server admin very unhappy).

In my rewrite of ExpGroupRenderData, I use the groupName information to only bind events to the elements in that group. Since ExpGroupRenderData will only fire once per page load per group, I don’t have to worry about “stacking” events.

function ExpGroupRenderData(htmlToRender, groupName, isLoaded) {
    $("#tbod"+groupName+"_").attr("isloaded",isLoaded)
		.html(htmlToRender)
		.show("fast",initjLoadMe("#tbod"+groupName+"_"));
}

Around to Help:
This is how I’m using around in my most recent tests. The aop.around method can tell us the parameters passed during the “invocation” (the instance of the target method) as arguments. However, we can’t simply fire our initjLoadMe function during the around method because the target method has not completed yet (and no HTML has been rendered).

So, what I’ve done is use the jQuery.data method to write the data I need to the main content div. Then I use the aop.after method to check for data attached to the main content div.

$.aop.around( {target: window, method: 'ExpGroupRenderData'},
	function (invocation) {
		if (invocation.arguments[2] == 'true') { //check to see if html loaded
			var group = "#tbod"+invocation.arguments[1]+"_"; //get groupname
			var elm = $("#MSO_ContentTable").get(0);
			$.data(elm, "group", group); //write data to div
		}
		return invocation.proceed();
	}
);

$.aop.after( {target: window, method: 'ExpGroupRenderData'},
  function() {
	var elm = $("#MSO_ContentTable").get(0);
	var group = $.data(elm, "group");
	if (group) {
		initjLoadMe(group); //if data exists run init(data)
		$.data(elm, "group", ""); //clear out data
	}
  }
);

Conclusion:
While more code, the end product also seems more elegant. For a modular approach to enhancing the SP interface, I could see using this technique. But for most projects, it’s probably a little more work than otherwise necessary.

Are there any areas you could use AOP to improve your SharePoint interface?

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

8 Responses to “JQuery for Everyone: Aspect-Oriented Programming with jQuery”
  1. George Winters says:

    I must confess, this is too complicated for my understanding. Can you show an example in action?

  2. AutoSponge says:

    @George,

    I’m almost ready with my first implementation. It will postback here when it publishes.

    Thanks,
    Paul

  3. Jeremy Thake says:

    I just written a post discussing jQuery as the SharePoint Band Aid. Be interested to hear your thoughts:
    I’ve just written a post discussing jQuery and SharePoint. Be interested to hear your thoughts… http://wss.made4the.net/archive/2009/02/23/jquery-the-sharepoint-band-aid.aspx

  4. Jeremy – I’ll jump over and comment on your post, but this is also part of a discussion on Joel’s blog last week. There is a camp of people who are requesting the removal of the Content Editor Web Part from the web part gallery selection because of jQuery and the things it can do.

    This might turn out to be a pretty good WWF Smackdown, which I’m not sure translates well for you guys down under, but means “Let’s get it on!”

    Mark

  5. AutoSponge says:

    I posted my reply. My opinions are my own and do not represent EUSP or its authors.

    jQuery is not the “problem.” Poor planning and governance coupled with overworked or under-trained administrators raises demand for something like jQuery.

Trackbacks

Check out what others are saying about this post...
  1. [...] JQuery for Everyone: Aspect-Oriented Programming with jQuery [...]

  2. On The Go Today: Video Recap, SharePoint Requests, And Monday Morning Reads…

    URL: On The Go Today: Video Recap, SharePoint Requests, And Monday Morning Reads Body: I am heading out…

  3. [...] you read the previous articles on AOP, you know I’m a fan. It took some serious digging for me to understand how the JQ-AOP plugin [...]




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!