jQuery Boilerplate Question about $.data()

I’ve been looking at jQuery Boilerplate and really like it; it makes good sense.

Towards the end you have:

// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[ pluginName ] = function ( options ) {
        return this.each(function() {
                if ( !$.data( this, "plugin_" + pluginName ) ) {
                        $.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
                }
        });
};

I get that it prevents multiple instantiations but isn’t the another good reason so you can more easily access certain matched elements. E.g. You could do:

$(".element").defaultPluginName({
    propertyName: "a custom value"
});

$("#element-1").data('plugin_defaultPluginName').yourOtherFunction();
$("#element-2").data('plugin_defaultPluginName').yourOtherFunction();

So you don’t need to return defaultPluginName to a variable and then filter; you can access any of the elements you want directly using data.

I’m still new to this, just trying to get my head around the reasoning behind it.

Finally, why is ( jQuery, window, document ) at the end rather than just (jQuery)? I know window and document are passed to speed things up but why have it at the end too?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.