Data Registry
In the context of plugin development, here's what we need to remember — the $.data() method lets you maintain a data registry tied to a specific element. So it's best to store all plugin data in this registry. If you need to save the plugin state — use data(), if you need a cache — use data(), if you need to save ... well, I think you get the picture. Here's another little example related to initialization:
function() { // init function
var init = $(this).data('mySimplePlugin');
if (init) {
return this;
} else {
$(this).data('mySimplePlugin', true);
return this.on('click.mySimplePlugin', function(){
$(this).css('color', options.color);
});
}
}Last updated