views:

30

answers:

1

Hi, I'm writing a jQuery plugin that works on a piece of JSON data object. This data needs to be calculated by the plugin only once, so I want to calculate it on the first call to the plugin and store it to be used in every subsequent call. My questtion is whether there's a standard and accepted methodology for storing data used by jQuery plugins. Assuming my plugin is:

jQuery.fn.myPlugin = function(){...}

I was thinking of storing it's calculated data in:

jQuery.myPlugin.data = {...}

Is this the acceptable way of going about it?

+1  A: 

I think storing it there is acceptable (or jQuery.fn.myPlugin.data for that matter)...or instead use your own ID in $.cache which is for storage but uses integer IDs for jQuery events and data, so you won't have any conflict, for example:

$.cache["myPlugin"] = myData;
//and to get:
var dataToUse = $.cache["myPlugin"];

The main reason I'd take this route is it eliminates the potential jQuery.something naming conflicts that could arise with future versions.

Nick Craver