【问题标题】:Call a function within a function to init调用函数内的函数进行初始化
【发布时间】:2012-06-28 15:44:47
【问题描述】:
;(function ($, w, d, config, undefined) {
$.fn.pluginName = function ( options, config ) {
    var pluginName = this;
    var defaults = {
        //defaults
    };
    var settings = $.extend({}, defaults, options);

    var methods = {
        init :  function ( settings, options ) {
            //init stuff here
        }
    }
})
})(jQuery, window, document)

// HTML looks like this
<script>
$('.item').pluginName({ methods : 'init' });
</script>

我是插件开发和一般对象的新手,但我试图在没有游泳的情况下深入学习。 :)

基本上,我想通过调用方法变量中的“init”函数来初始化我的插件。我的插件名称是“pluginName”。

我无法调用“init”fn,因为它位于名为“methods”的变量中。

另外,为了更进一步,我需要收集页面上的所有“项目”类,并在里面设置一个数据变量。在我的 init 函数中,我有以下内容:

return this.each(function(){

    var $this       = $(this),
    data        = $this.data('pluginName');

    if ( ! data ) {
        $(this).data('pluginName', {
        target : $this
        });

    }
}).bind(this);

以上返回“this.each 不是函数”

任何帮助将不胜感激!非常感谢!!

【问题讨论】:

    标签: jquery function variables plugins init


    【解决方案1】:

    为了让你不必为方法调用传入对象,我通常使用这种格式:

    (function($) {
        function doSomething() {
            // Only callable in this plugin's context (I think)
        }
    
        var methods = {
            init: function (options) {
                // Do whatever for init!
                doSomething();
            },
    
            anotherMethod: function (options) {
                // Some other method
                doSomething();
            }
        };
    
        $.fn.pollServer = function(method) {
            var args = arguments;
            var argss = Array.prototype.slice.call(args, 1);
    
            return this.each(function () {
                $this = $(this);
                if (methods[method]) {
                    methods[method].apply($this, argss);
                }
                else if (typeof method === "object" || !method) {
                    methods.init.apply($this, args);
                }
                else {
                    $.error("Method " + method + " does not exist on jQuery.pollServer");
                }
            });
        };
    })(jQuery);
    

    你可以像这样访问它:

    $("#div").pollServer({});
    $("#div").pollServer("init", {}); // Same as above line
    
    $("#div").pollServer("anotherMethod", {});
    

    return this.each() 中的所有内容都决定了调用什么方法,并将“this”变量设置为所选的 jQuery 元素。它还将其他参数传递给方法。

    希望这会有所帮助!

    【讨论】:

    • 这太棒了!!谢谢!我会处理这个。我已经写了一个不同的模式,但你的似乎更合乎逻辑。一旦我实施了您的解决方案,我将再次发表评论并让您知道它是如何工作的!非常感谢!
    • 没问题!我在 jQuery 网站上的某处发现了类似的东西,但对其进行了一些修改以做我想做的事。它对我有用,所以我希望它对你有帮助!如果您需要更多类似这样的指导,请告诉我
    • 好的,所以我不想开始弄乱我的代码。 init 函数工作得很好,谢谢!所以我有两个新问题,我认为我是 GOLDEN ......首先。 如何从“init”中调用函数?记住它在“methods”变量中。 另外,我如何再次使用“methods”变量中的函数从“pollServer()”中调用函数?您可以通过参数传递给出的任何示例都将非常受欢迎!你已经帮我加载了!谢谢!
    • 我添加了下面的代码以便更好地参考。这不是 100%,我用其他功能部件保存了不同的文件。一旦我能够弄清楚这一点,我将能够添加这些功能:)
    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2020-08-30
    • 2013-04-01
    • 2012-03-29
    相关资源
    最近更新 更多