jQuery 插件:添加回调功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2534436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:43:50  来源:igfitidea点击:

jQuery Plugin: Adding Callback functionality

jqueryfunctioncallback

提问by dclowd9901

I'm trying to give my plugin callback functionality, and I'd like for it to operate in a somewhat traditional way:

我正在尝试提供我的插件回调功能,我希望它以某种传统的方式运行:

myPlugin({options}, function() {
    /* code to execute */
});

or

或者

myPlugin({options}, anotherFunction());

How do I handle that parameter in the code? Is it treated as one full entity? I'm pretty sure I know where I'd place the executory code, but how do I get the code to execute? I can't seem to find a lot of literature on the topic.

我如何处理代码中的那个参数?它是否被视为一个完整的实体?我很确定我知道将执行代码放在哪里,但是如何让代码执行呢?我似乎找不到很多关于这个主题的文献。

回答by David Hellsing

Just execute the callback in the plugin:

只需在插件中执行回调:

$.fn.myPlugin = function(options, callback) {
    if (typeof callback == 'function') { // make sure the callback is a function
        callback.call(this); // brings the scope to the callback
    }
};

You can also have the callback in the options object:

您还可以在选项对象中使用回调:

$.fn.myPlugin = function() {

    // extend the options from pre-defined values:
    var options = $.extend({
        callback: function() {}
    }, arguments[0] || {});

    // call the callback and apply the scope:
    options.callback.call(this);

};

Use it like this:

像这样使用它:

$('.elem').myPlugin({
    callback: function() {
        // some action
    }
});

回答by Felix Kling

I don't know if I understand your question correctly. But for the second version: This would call anotherFunctionimmediately.

我不知道我是否正确理解你的问题。但是对于第二个版本:这会anotherFunction立即调用。

Basically your plugin should be some kind of function that looks like this:

基本上你的插件应该是某种看起来像这样的功能:

var myPlugin = function(options, callback) {
    //do something with options here
    //call callback
    if(callback) callback();
} 

You have to provide a function objectas callback, so either function(){...}or anotherFunction(without ()).

您必须提供一个函数对象作为回调,因此要么function(){...}anotherFunction(没有())。

回答by Sam Potts

Bringing back a blast from the past.

带回过去的爆炸。

Worth noting that if you have two arguments passed, for example:

值得注意的是,如果您传递了两个参数,例如:

$.fn.plugin = function(options, callback) { ... };

Then you call the plugin without the options argument but with a callback then you'll run into issues:

然后你在没有 options 参数的情况下调用插件但是有一个回调然后你会遇到问题:

$(selector).plugin(function() {...});

I use this to make it a little more flexible:

我用它来使它更灵活一点:

if($.isFunction(options)) { callback = options }

回答by Shubham Kumar

I think this might help you

我想这可能对你有帮助

// Create closure.
(function( $ ) {
  
   // This is the easiest way to have default options.
 
        var settings = $.extend({
            // These are the defaults.
 
            onready: function(){},
 
            //Rest of the Settings goes here...
        }, options );
 
    // Plugin definition.
    $.fn.hilight = function( options ) {
 
        //Here's the Callback
        settings.onready.call(this);
 
        //Your plugin code goes Here
    };
  
// End of closure.
  
})( jQuery );

I had shared a article about Creating your Own jQuery Plugin.I think you should check that http://mycodingtricks.com/jquery/how-to-create-your-own-jquery-plugin/

我分享了一篇关于创建你自己的 jQuery 插件的文章。我认为你应该检查一下 http://mycodingtricks.com/jquery/how-to-create-your-own-jquery-plugin/

回答by SLaks

Change your plugin function to take a second parameter. Assuming that the user passes a function, that parameter can be treated as a regular function.
Note that you can also make the callback a property of the options parameter.

更改您的插件函数以采用第二个参数。假设用户传递了一个函数,该参数可以被视为一个常规函数。
请注意,您还可以使回调成为 options 参数的属性。

For example:

例如:

$.fn.myPlugin = function(options, callback) {
    ...

    if(callback)        //If the caller supplied a callback
        callback(someParam);

    ...
});

回答by Draka

An example bit late, but it can be useful. Using arguments can create the same functionality.

一个例子有点晚了,但它可能很有用。使用参数可以创建相同的功能。

$.fn.myPlugin = function() {
    var el = $(this[0]);
    var args = arguments[0] || {};
    var callBack = arguments[1];
    .....
    if (typeof callback == 'function') {
        callback.call(this);
    }
}