Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery - The Power of Plug-ins

 

Learning jQuery - The Power of Plug-ins

The alternateRowColors() function that we wrote is a perfect candidate to become a jQuery plug-in. In fact, any operation that we wish to apply to a set of DOM elements can easily be expressed as a plug-in. In this case, we only need to modify our existing function a little bit:



Sample Code
  1. jQuery.fn.alternateRowColors = function() {
  2. $('tbody tr:odd', this).removeClass('even').addClass('odd')
  3. $('tbody tr:even', this).removeClass('odd').addClass('even')
  4. return this
  5. }
Copyright exforsys.com



We have made three important changes to the function.


  • It is defined as a new property of jQuery.fn rather than as a standalone function. This registers the function as a plug-in method.
  • We use the keyword this as a replacement for our $table parameter. Within a plug-in method, this refers to the jQuery object that is being acted upon.
  • Finally, we return this at the end of the function. The return value makes our new method chainable.

More information on writing jQuery plug-ins can be found in Chapter 10. There we will discuss making a plug-in ready for public consumption, as opposed to the small example here that is only to be used by our own code.


With our new plug-in defined, we can call $table.alternateRowColors(), which is a more natural jQuery syntax, intead of alternateRowColors($table).


Performance Concerns

Our code works, but is quite slow. The culprit is the comparator function, which is performing a fair amount of work. This comparator will be called many times during the course of a sort, which means that every extra moment it spends on processing will be magnified.


The actual sort algorithm used by JavaScript is not defined by the standard. It may be a simple sort like a bubble sort (worst case of Θ(n2) in computational complexity terms) or a more sophisticated approach like quick sort (which is Θ(n log n) on average). In either case doubling the number of items increases the number of times the comparator function is called by more than double.


The remedy for our slow comparator is to pre-compute the keys for the comparison. We begin with the slow sort function:


Sample Code
  1. rows.sort(function(a, b) {
  2. keyA = $(a).children('td').eq(column).text().toUpperCase()
  3. keyB = $(b).children('td').eq(column).text().toUpperCase()
  4. if (keyA < keyB) return -1
  5. if (keyA > keyB) return 1
  6. return 0
  7. })
  8. $.each(rows, function(index, row) {
  9. $table.children('tbody').append(row)
  10. })
Copyright exforsys.com



We can pull out the key computation and do that in a separate loop:


Sample Code
  1. $.each(rows, function(index, row) {
  2. row.sortKey = $(row).children('td').eq(column).text().toUpperCase()
  3. })
  4. rows.sort(function(a, b) {
  5. if (a.sortKey < b.sortKey) return -1
  6. if (a.sortKey > b.sortKey) return 1
  7. return 0
  8. })
  9. $.each(rows, function(index, row) {
  10. $table.children('tbody').append(row)
  11. row.sortKey = null
  12. })
Copyright exforsys.com



In the new loop, we are doing all of the expensive work and storing the result in a new property. This kind of property, attached to a DOM element but not a normal DOM attribute, is called an expando. This is a convenient place to store the key since we need one per table row element. Now we can examine this attribute within the comparator function, and our sort is markedly faster.



NOTE: We set the expando property to null after we're done with it to clean up after ourselves. This is not necessary in this case, but is a good habit to establish because expando properties left lying around can be the cause of memory leaks. For more information, see Appendix C.



Read Next: jQuery - Finessing the Sort Key



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape