【问题标题】:jQuery plugin + AMD = how to access functions?jQuery 插件 + AND = 如何访问函数?
【发布时间】:2013-12-04 15:42:28
【问题描述】:

我正在 AMD 环境中打包我的 jQuery 插件。这是我的样板文件,

!function(root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else {
        factory(root.jQuery);
    }
}(this, function($) {

    var defaults = {
       target: ''
    };

    var myPlugin = function(options) {
        options = $.extend(true, {}, defaults, options);

        return options;
    };

    myPlugin.prototype = {
        init: function(options) {
            return options;
        }
    };

    $.fn.myPlugin = myPlugin;

});

console.log($.fn.myPlugin.init());

错误,

TypeError: $.fn.myPlugin.init 不是函数

console.log($.fn.myPlugin.init());

任何想法我做错了什么?以及如何访问myPlugin.prototype = {...}中的函数?

编辑:

用这个boilerplate测试过,

console.log($('.test li').plugin({
        test: 'option1',
        test2: 'option2'
    }));

结果,

Object[] // why is it empty?

console.log($.fn.plugin.someMethod());

结果,

TypeError: $.fn.plugin.someMethod 不是函数

console.log($.fn.plugin.someMethod());

还有,

// Plugin methods and shared properties
    Plugin.prototype = {
        // Reset constructor - http://goo.gl/EcWdiy
        constructor: Plugin,

        someMethod: function(options) {
            return options;
        }
    };

console.log($.fn.plugin.Plugin.prototype.someMethod("hello world"));

结果,

hello world

还有,

var instance = $('.element').data('plugin');
    instance.someMethod("hello world");

结果,

TypeError: instance is null // 这是什么意思?它应该返回“hello world”,不是吗?

instance.someMethod("hello world");

编辑 2:

var plugin = $('.element').plugin();
    var instance = $('.element').data('plugin',plugin);
    console.log(instance); // returns - Object[]
    console.log(instance.someMethod("hello world"));

结果,

TypeError: instance.someMethod 不是函数

console.log(instance.someMethod("hello world"));

【问题讨论】:

  • 例如:$(body).myPlugin.init({mobile:true});

标签: javascript jquery requirejs js-amd


【解决方案1】:

您似乎并没有真正创建 myPlugin 的实例,而是尝试静态访问可能是也可能不是您所追求的方法。

我发现每次使用插件时创建一个插件对象的实例会更好。一个例子:

!function(root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else {
        factory(root.jQuery);
    }
}(this, function($) {
    'use strict';

    var defaults = {

    };

    var Plugin = function(element, options) {
        this.element = element;
        this.options   = options;
    };

    Plugin.prototype = {
        constructor: Plugin,

        someMethod: function() {

        }
    }

    // Create the jQuery plugin
    $.fn.plugin = function(options) {
        options = $.extend(true, {}, defaults, options);

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

    // Expose defaults and Constructor
    $.fn.plugin.defaults = defaults;
    $.fn.plugin.Plugin   = Plugin;
});

从这里 - https://gist.github.com/simonsmith/4353587

现在你可以像这样使用插件了:

require(['jquery', 'jquery.plugin'], function($) {
    $('.test li').plugin({
        test: 'option1',
        test2: 'option2'
    });
});

对象的实例保存在数据属性中,因此始终可以访问它。 Herotabs 使用这种技术:

var instance = $('.tabs').herotabs().data('herotabs');
instance.nextTab();

【讨论】:

  • 谢谢。但我仍然不知道如何使用它。请参阅我上面的编辑。谢谢。
  • 您仍然尝试静态使用它,而不是先创建插件。这是故意的吗?请参阅我的答案部分,其中我向您展示了如何访问 .nextTab() 方法作为示例。如果您真的想静态使用它,那么$.fn.plugin.Plugin.prototype.someMethod() 将起作用
  • 不,不是。我只是对标准方式和 RequireJS 感到困惑。我还通过首先创建插件的实例对其进行了测试,但我仍然收到错误消息。请再次查看我的编辑。谢谢。
  • 啊,好吧,所以要让这个var instance = $('.element').data('plugin'); 工作,你需要先调用插件,即$('.element').plugin() 一旦你这样做了,它会在 data 属性中保存一个实例,如上所示第 44 行 - goo.gl/eFoWH8
  • 谢谢。但很抱歉我还是迷路了。你能再看看我上面的编辑2吗?谢谢!
【解决方案2】:
    var plugin = $('.element').plugin();
    var instance = $('.element').data('plugin');
    console.log(instance);
    console.log(instance.someMethod("hello world"));

结果,

Object { element={...}, options={...}, constructor=function(), more...}

hello world

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多