Issue with creating an jQuery plugin without selector
I'm working on a jQuery plugin which does not require a selector so I'm
just adding it to the global jQuery namespace.
I'm having issues with sharing internal instance variables between my
plugin's public methods. For example, I would like to be able to access
$.MyPlugin.settings from the .init method.
Here's the plugin code:
(function($) {
$.MyPlugin = function(options) {
var defaults = {
username: '',
password: ''
}
var plugin = this;
plugin.settings = {};
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
}
plugin.init();
}
$.MyPlugin.init = function(callback) {
console.log(this.settings); // undefined
callback();
}
}(jQuery));
In main.js, I have:
$(document).ready(function() {
$.MyPlugin({
username: 'myuser',
password: 'mypass'
});
$.MyPlugin.init(function() {
console.log('hi');
});
This returns:
undefined
hi
No comments:
Post a Comment