Chapter 5. Collection plugins

 

This chapter covers

  • Defining collection plugins
  • Using a plugin framework
  • Applying the design principles
  • Creating a complete collection plugin

Now that you’ve read the theory behind designing and implementing a plugin, you’ll see how it’s implemented in practice. To make things more concrete, you’ll create a relatively simple plugin that provides a useful service, while still being complex enough to show most of the techniques involved in any plugin.

The plugin you’ll build complements existing functionality provided by the browser. Normal text input fields have a maxlength attribute that allows you to limit how much text may be entered into a field. Such a restriction helps enforce limits that may be imposed by databases and other storage mechanisms. But the multiline textarea field has no such attribute and allows unlimited text entry. To address this situation, you can create a collection plugin to control the amount of acceptable text, and provide valuable feedback along the way.

5.1. What are collection plugins?

As you may recall from chapter 3, jQuery usually operates under a select and act pattern. You find the elements of interest, either directly by using selectors and filters, or by traversing the DOM from an existing selection, and then apply some functionality to them. For example, to hide all paragraphs with a class of note, you’d use the following code:

$('p.note').hide();

5.2. A plugin framework

 
 

5.3. Defining your plugin

 
 

5.4. Attaching to an element

 

5.5. Setting options

 
 

5.6. Adding event handlers

 

5.7. Adding methods

 
 
 
 

5.8. Removing the plugin

 
 

5.9. Finishing touches

 
 
 
 

5.10. The complete plugin

 
 

5.11. Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest