4

我写了一个 jq 插件,我首先要在其中初始化选定的元素(所有这些元素)。稍后在代码中,我想获取由方法生成的字符串。但它返回的只是对象,而不是我的字符串。

我在互联网上做了很多研究,但一方面我不知道如何使插件“可链接”,另一方面又不知道如何“返回任何值”。

你怎么看?

(function($){
    var methods = {
        init: function(val){
            // Actions to initialize ALL selected Elements
        }
        ,
        returner: function(val){
            alert('working with every selected element: '+$(this).width());
            // return anything
            return 'STRING PRODUCED BY THIS FUNCTION!';
        }
    }

    $.fn.myplug = function(method){
        var args = arguments;
        var init = 'init-myplug';
        return this.each(function(){
            var is_init = $(this).data(init);
            if (is_init && methods[method]){
                return methods[method].apply($(this), Array.prototype.slice.call(args, 1));
            } else if (!is_init && (typeof method === 'object' || !method)){
                $(this).data(init, true);
                return methods.init.apply($(this), Array.prototype.slice.call(args, 0));
            }
        });
    };
})(jQuery);

$('.selects_5_elements').myplug(); // init

$('.selects_5_elements').myplug('returner'); // get the string
4

2 回答 2

2

The first step would be to gather the results from your method. At the moment you are trying to return the result of the method through the each callback, which isn't going to work (the return value in the each callback is used to break out of the loop).

I would suggest gathering all the results in an array like this:

var results = [];
this.each(function(){
    var is_init = $(this).data(init);
    if (is_init && methods[method]){
        results.push(methods[method].apply($(this), Array.prototype.slice.call(args, 1)));
    } else if (!is_init && (typeof method === 'object' || !method)){
        $(this).data(init, true);
        results.push(methods.init.apply($(this), Array.prototype.slice.call(args, 0)));
    }
});

At this point you could simply return the results array. But if you want to allow some methods to be chainable, while others return the results of the method call, then you would need to check the method name to decide what value to return.

if (method == 'returner')
    return results;
else
    return this;

Also, note that we are returning an array here, not a string. The reason is that your method is going to be called for every element matching the selector. So if you have five matching elements you are going to get an array of 5 strings.

If you really just want a single string back, then you need to decide how to handle all the duplicates. Do you want to join all the strings together? Or only return the first string? Or only the last string? All options are easy enough to do - which you choose will depend on your requirements.

于 2013-07-22T15:55:54.807 回答
0

您唯一需要做的就是将其放入每个回调中:

if ( args.length === 0 ) {
   // Init your plugin
   return this;
}

if ( args[0] === "returner" ) {
   // Generate your string
   return "STRING PRODUCED BY THIS FUNCTION!";
}
于 2013-07-26T12:51:35.070 回答