公共函数中的Jquery插件回调

时间:2016-04-19 11:51:54

标签: jquery function jquery-plugins

我一直在为我的插件使用这个结构(样板文件)。

(function($) {

    $.pluginName = function(element, options) {

        var defaults = {
            foo: 'bar',
            onFoo: function() {}
        }

        var plugin = this;

        plugin.settings = {}

        var $element = $(element),
             element = element;

        plugin.init = function() {
            plugin.settings = $.extend({}, defaults, options);
            // code goes here
        }

        plugin.foo_public_method = function() {
            // code goes here
        }

        var foo_private_method = function() {
            // code goes here
        }

        plugin.init();

    }

    $.fn.pluginName = function(options) {

        return this.each(function() {
            if (undefined == $(this).data('pluginName')) {
                var plugin = new $.pluginName(this, options);
                $(this).data('pluginName', plugin);
            }
        });

    }

})(jQuery);

我想以这种方式向我的foo_public_method添加一个回调

plugin.foo_public_method = function(data, callback) {
   // code goes here
   if (typeof callback == 'function') {
      callback.call(plugin);
   }
}

但是当我尝试使用回调

调用此公共函数时出现错误
$('#element').data('pluginName').foo_public_method(data, callback: function(){//some code});

我该怎么办?感谢。

0 个答案:

没有答案
相关问题