The problem you are facing is that the data is not stored in every instance of your plugin. This is the default behavior, but you can get around it using .data()
:
(function ($) { $.MyPlugin = function (options) { var defaults = { username: '', password: '' }, plugin = this, options = options || {}; plugin.init = function () { var settings = $.extend({}, defaults, options); $.data(document, 'MyPlugin', settings); } plugin.init(); } $.MyPlugin.init = function (callback) { console.log($.data(document, 'MyPlugin')); callback(); } }(jQuery));
Saves the settings of your plugin in the document
element using the jQuery .data()
function. This prevents you from using multiple instances of your plugin with separate parameters, though, since each repeated declaration of the $.MyPlugin
parameters will overwrite the previous ones.
I base my answer on the assumption that you want to use different public methods in your entire script and that they all share the original parameters. If you only want to name the plugin once, then Robert is probably in the right direction.
Here it works: http://jsfiddle.net/pPJYx/
source share