Supporting the Metadata Plugin - J Query

Problem
Several plugins utilize the metadata plugin to pass custom options into their methods. How can integration with the metadata plugin be constructed?

Solution

Leveraging the metadata plugin is as simple as checking whether the plugin is available and then extending your plugin options with the metadata parameters. Using this technique, you can supply default options when making the call to your plugin and override those default options for each object to be operated on through the metadata written into the markup:

Discussion

Including the metadata plugin is a great example of how jQuery plugins can build off of one another. The jQuery plugin ecosystem is vast, and chances are there are other plugins that you can utilize.
To include and use the metadata plugin, you first must actually include it into your script. The metadata plugin is hosted along with jQuery at Google Code. The metadata plugin works by allowing you to embed additional data into your HTML, while still producing valid HTML. We take advantage of this by allowing users to embed element specific options into the class element of the items we can operate on.
The options are embedded into the HTML using standard JSON. All of the options may be embedded, or none may be embedded; it’s up to your users. There are several other methods and options for using the metadata plugin that are described on its documentation page.
Within our plugin, we first check to see whether a user has included the metadata plugin. This is done to ensure that we keep this additional feature optional and to provide backward compatibility, if necessary. Because the metadata plugin operates on a single element, we split up how we handle options. The first step is to use the options provided when the plugin was called. These options are extended with our default options, creating our starting point for this first instantiation of our plugin. The second step is to extend those locally default options with the metadata that may be defined for each element. All that is required is for us to extend our locally default options with the metadata options, if the metadata plugin exists.
The metadata plugin provides another option for users of your plugin to pass in options. Providing options to potential users is a great way to show that you are committed to your plugin, being a good citizen of the jQuery ecosystem. The metadata plugin is also a great way to offer your users the ability to write less code by embedding custom options in to the HTML elements.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics