Chief Alchemist
Chief Alchemist

Reputation: 644

How can I access a plugin's user defined options in a public function?

I have a plugin and within it I have a public function ($fn.myplugin.mypublicfunct). My intention is to have mypublicfunct called from a separate .ajaxSuccess outside of myplugin. Thus the want for a public function.

Scenario: myplugin does its thing...form submit via Ajax is different plugin...when that plugin does its .ajaxSuccess I want to call mypublicfunct but be able to use the user options / defaults from myplugin.

Does that make sense? Am I asking the right question the right way?

I tried putting

var opts = $.extend({}, $.fn.myplug.defaults, options);

within the public function and got an error for options not defined (or similar). I'm Google searching up a storm looking for an example, solution, etc. and still I got nuttin'.

Upvotes: 2

Views: 97

Answers (1)

net.uk.sweet
net.uk.sweet

Reputation: 12431

I'll qualify this first by saying that I'm a relative newbie to jQuery plug-in development. Further, this is something I have struggled with myself, so it's a +1 from me for a good question.

What I have tended to do is store my options object on the data property of the selected element (name-spaced with the plugin name):

var data = {
    options: options,
    otherData: otherData
};

$(this).data('myPlugin', data);   

The object is then accessible in the public function like so:

var options = $(this).data('myPlugin').options;    

It feels a bit clunky but it does the job.

Upvotes: 1

Related Questions