jQuery - Easy Plugins
jQuery - Easy Plugins
Written by Ian Elliot   
Monday, 11 February 2013
Article Index
jQuery - Easy Plugins
Chaining
Avoiding Collisions

Avoiding Collisions

Up to this point our plugin has been defined in the simplest way possible so that we can concentrate on how it works. In practice you would use a standard way of setting up the plugin that avoid the problem of clashes with the use of $. The problem is caused because other libraries use the $ as a shorthand symbol and so jQuery gives the user the option of selecting a different symbol. So far we have use $ in all out plugin code an this will mean if will fail if the compatibility option has been used and jQuery is using some other symbol.

The correct way to set up your code is to define a local definition of $ that is safe from changes else where. The standard way of doing is is to use an IIFE:

(function($){
your plugin code using $
})(jQuery);


This defines the parameter $ to be the jQuery object so that your code can work in the usual way. Your plugin should also add itself to the $.fn object within this function which should be executed before anything attempts to use your plugin.

Using this form the complete table plugin is:

 

(function($) {
 $.fn.table = function(options) {
  var settings = $.extend({n: 2, m: 2}, options);
  var n = settings.n;
  var m = settings.m;

  var table = $("<table>");
  var tbody = $("<tbody>");
  var row = $("<tr>");
  var cell = $("<td>");
  for (var i = 0; i < n; i++) {
   var tempRow = row.clone();
   for (var j = 0; j < m; j++) {
    tempRow.append(cell.clone().html(i+","+j));
   }
   table.append(tbody);

   this.first().append(table);
   return this;
  }
 })(jQuery);


The modifications needed to make it into a plugin were very few and the process was generally easy.

Where Next

There are some advanced topics still to cover. We haven't described how to work safely with events in plugins and we haven't looked at how plugins can save data from one invocation to the next. Both are fairly easy, but are seldom necessary so look them up when you need to.

There is also the topic of how to make your plugin available to the jQuery community, and this is just a matter of setting up the project on gitHub and creating the necessary files to let people know what is in your creation. See the jQuery website for details.

 

More Information

http://jquery.com/

jquerysq

Now Available as a Print Book: 

 

smallcoverjQuery

You can buy it from:

USA and World     Amazon.com
Canada            Amazon.ca

UK                Amazon.co.uk
France            Amazon.fr
Germany           Amazon.de
Spain             Amazon.es
Italy             Amazon.it

 

 

 

The Core UI

  1. Understanding jQuery (Book Only)
  2. The DOM And Selectors
  3. CSS Selectors
  4. The jQuery Object (Book Only)
  5. Filters
  6. Advanced Filters - Traversing The DOM
  7. Modifying DOM Objects
  8. Creating And Modifying The DOM
  9. jQuery Data
  10. Forms
  11. Function Queues
  12. Animation
  13. jQuery UI
  14. jQuery UI Custom Control - Widget Factory
  15. jQuery - Easy Plugins
  16. Getting Started With QUnit Testing

Now Available as a Print Book:

 

jquery2coverYou can buy it from:

USA and World   Amazon.com
Canada          Amazon.ca
UK              Amazon.co.uk
France          Amazon.fr
Germany         Amazon.de
Spain           Amazon.es
Italy           Amazon.it

 

 

 

Events, Async & AJAX

  1. Events, Async & Ajax (Book Only)
  2. Reinventing Events
  3. Working With Events
  4. Asynchronous Code
  5. Consuming Promises
  6. Using Promises 
  7. WebWorkers
  8. Ajax the Basics - get
  9. Ajax the Basics -  post
  10. Ajax - Advanced Ajax To The Server
  11. Ajax - Advanced Ajax To The Client
  12. Ajax - Advanced Ajax Transports And JSONP
  13. Ajax - Advanced Ajax The jsXHR Object
  14. Ajax - Advanced Ajax Character Coding And Encoding 

 

Banner


Getting Started With jQuery - Advanced Ajax Characters & Encoding

One of the biggest problems you encounter in using Ajax is the dreaded character encoding. No matter what data format you select, the data is actually transmitted as text. But it isn't as simple as th [ ... ]



Speed dating - The Art of the JavaScript Date Object

JavaScript's way of working with dates is simple but perhaps this is part of the problem. The Date object is so simple that it can be difficult to work out how to do things like date arithmetic. Find  [ ... ]


Other Articles

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin.

 
 

 

blog comments powered by Disqus

Banner


Getting Started With jQuery - Advanced Ajax Characters & Encoding

One of the biggest problems you encounter in using Ajax is the dreaded character encoding. No matter what data format you select, the data is actually transmitted as text. But it isn't as simple as th [ ... ]



Speed dating - The Art of the JavaScript Date Object

JavaScript's way of working with dates is simple but perhaps this is part of the problem. The Date object is so simple that it can be difficult to work out how to do things like date arithmetic. Find  [ ... ]


Other Articles



Last Updated ( Sunday, 29 January 2017 )
 
 

   
Banner
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.