Ответ 1
Вам нужно будет получить ссылку на класс, чтобы вызвать его метод с этой структурой плагина.
$(document).ready(function() {
var test = $("#foo").test().data("plugin_test");
test.goodbye();
});
Чтобы сделать то, что вы хотите, вы должны избавиться от document.write, чтобы проверить его.
;
(function($, window, document, undefined) {
var pluginName = 'test';
var defaults;
function Plugin(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function(name) {
this.hello();
},
hello: function(name) {
console.log('hello');
},
goodbye: function(name) {
console.log('goodbye');
}
}
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
else if ($.isFunction(Plugin.prototype[options])) {
$.data(this, 'plugin_' + pluginName)[options]();
}
});
}
})(jQuery, window, document);
$(document).ready(function() {
$("#foo").test();
$("#foo").test('goodbye');
});
Посмотрите на консоль для информации.