Object JavaScript – Building a Reusable Stateless jQuery Plugin

6327_image_58FAEDFAIn this post, you will learn step-by-step to build your own custom, reusable, testable jQuery Plugin.

There are times where you will want to reuse code that performs a series of operations on a selection.

For example, you may want to embed information a span element and then have that information displayed in a references section near the end of the document. In this case, the jQuery plugin is stateless.

In the next post, Building Stateful jQuery UI Plugin Using Widget Factory, you will see how to create a stateful jQuery plugin using jQuery Widget. And you will see how the widget is a better solution for plugins that require user interaction, because the Widget factory helps you maintain state.

Stateless Widgets

jQuery provides a lot of help for stateless widgets. For example:

.text( "hello" )

Here you are setting the text somewhere in the DOM and you do not need to maintain state. In fact, jQuery provides a way to create plugins.

The jQuery plug-in documentation provides a good example for a simple use case (making links green).

In this post, you will learn one that is a bit more complex. You will learn step by step on how to build a jQuery footnote plugin. Interestingly, the footnote plugin does not require you to maintain state. In other words, you provide information regarding the plug in sate once, and it does not need to change.

Setting Up the First Use Case

First, you will need to create an HTML page and  start jQuery.

NOTE: For details on how to get jQuery and start jQuery for commercial product, see What is jQuery and How to Start using jQuery? and Load jQuery with Javascript and use jQuery. And in your ASP.NET projects, you will want to minify it.

For our purposes, we’ll add jQuery to a Visual Studio project using NuGet and then reference it from our project. And add some text.

Next, let’s define our first use case. Take the text in the .footnote span and make it red.

Protect jQuery

Put it into a file named jquery-footnoter-0.0.1.js in the Scripts/footnotes folder.

First, you will want your plugin to work well with others. To protect the $ alias used by JQuery and use it inside your plugin, you will want  to put all of your code inside of an Immediately Invoked Function Expression, and then pass the function jQuery, and name the parameter $:

Chaining

Next, let’s do something simple to be sure we have each of the footnote items. And be sure to return the items that are being manipulated so they can be chained to the next step. chaining gives your jQuery plugin the ability to add an additional feature.

In this case, you can return the footnote spans as the return value.

You can then chain the result to another value.

Which results like this:

image

 

Moving Elements Around

You can use jQuery to reorder the elements in the DOM. When you write the code, you will want to understand that you are moving elements and not text.

image

Providing Options

But the plug-in is not really reusable. So far, it requires that the footnotes are blue and that they are appended to the #references div.

Sample Code

The following is the sample code:

References

Advertisements

One comment

  1. Pingback: Object JavaScript – Building Stateful jQuery UI Plugin Using Widget Factory | DevDays®