I'm trying to write a plugin that will add a few methods to a jQuery wrapper object. Basically, I want to initialize it like this:
var smart = $('img:first').smartImage();
The 'smartImage' plugin would attach 2 methods to the object referenced by 'smart', so I'll be able to do something like:
smart.saveState();
// do work
smart.loadState();
Unfortunately, I can't figure out how to attach these 2 methods to the wrapper object. The code I have follows the typical jQuery plugin pattern:
(function($)
{
$.fn.smartImage = function()
{
return this.each(function()
{
$(this).saveState = function() { /* save */ }
$(this).loadState = function() { /* load */ }
}
}
}
After I call smartImage(), neither 'saveState' nor 'loadState' is defined. What am I doing wrong?