简体   繁体   English

如何在JavaScript中向回调函数添加其他参数

[英]how to add additional parameters to a callback function in javascript

In my javascript XMPP project there is a core part and a plugin part. 在我的javascript XMPP项目中,有一个核心部分和一个插件部分。 The core part must be independent of the plugin part. 核心部分必须独立于插件部分。 I am trying to call a core-function from the plugin part, that itself needs to call a plugin-function. 我正在尝试从插件部分调用核心功能,而它本身需要调用插件功能。 since the core part by concept does not know of any plugin-functions, I am trying to hand the plugin-function over as a callback function. 因为从概念上讲核心部分不知道任何插件功能,所以我试图将插件功能作为回调函数移交给。 Here it goes: 它去了:

There is the strophe-library and the muc-plugin which holds the join - function . strophe库muc 插件 ,它们具有join 函数

I added a function joinMuc myself to the core, so that it can have a callback, which is not provided by the original function: 我自己在内核中添加了一个函数joinMuc ,以便它可以有一个回调,而原始函数没有提供该回调:

core = {
    joinMuc: function(room, onMessage, onPresence, onRoster, result) {
        MX.connection.muc.join(
            room,
            jQuery.jStorage.get('settings').username,
            onMessage,
            onPresence,
            onRoster
        );
        if (result) result(room);
    },
}

I call core.joinMuc and pass the parameters and callback functions: 我调用core.joinMuc并传递参数和回调函数:

plugin = {
    doHtml: function(result) {
        //…
    },
    doPlain: function(result) {
        //…
    },
    joinGame: function(chatroom){
        core.joinMuc(
            chatroom,
            core.onMessage,
            core.onPresence,
            core.onRoster,
            function(result){
                //…
            }
        );
    }
}

core.onMessage will get the actual xmpp-message as a parameter from the join - function , but now here is the tricky part: I actually need three parameters for core.onMessage to hand over the two plugin functions of which one is supposed to to be run after core.onMessage decided what kind of message it is: core.onMessage将获得实际的XMPP消息从参数join - 功能 ,但现在这里是棘手的部分:事实上,我需要三个参数core.onMessage交出其中一个应该是这两个插件功能在core.onMessage确定是哪种消息后运行:

core = {
    onMessage: function (message, handleHTMLMessage, handlePlainMessage){
        var rawMessageBody = $(message).find('body').text();
        var parsedMessageHtml = $.parseHTML(rawMessageBody)[0];
        if ( parsedMessageHtml.nodeName.toLowerCase() == 'foo' ) {
            handleHTMLMessage(parsedMessageHtml);
        } else {
            handlePlainMessage(rawMessageBody);
        }
        return true;
    },
}

So now my question is: how can I call core.onMessage with two extra parameters? 所以现在我的问题是:如何使用两个额外的参数调用core.onMessage I tried to call core.onMessage(plugin.doHtml,plugin.doPlain) as parameter of core.joinMuc but that ended up in an infinite loop that crashed the browser. 我试着打电话给core.onMessage(plugin.doHtml,plugin.doPlain)作为参数core.joinMuc但在一个无限循环崩溃的浏览器结束了。

You should be able to use an anonymous function that ends up calling core.onMessage with the required parameters: 您应该能够使用匿名函数,该匿名函数最终以所需的参数调用core.onMessage

core.joinMuc(
    chatroom,
    function(message) {            
        core.onMessage(message, plugin.doHtml, plugin.doPlain);
    },
    core.onPresence,
    core.onRoster,
    function(result){
        //...
    }
);

Is this what you want? 这是你想要的吗?

One possibility: 一种可能性:

// probably namespaced somewhere, but this is the idea
var createMessageHandler = function(handleHTMLMessage, handlePlainMessage) {
    return function(message) {
        core.onMessage(message, handleHTMLMessage, handlePlainMessage);
    };
};

Then: 然后:

plugin = {
    doHtml: function(result) { /* ... */},
    doPlain: function(result) { /* ... */},
    joinGame: function(chatroom){
        core.joinMuc(
            chatroom,
            createMessageHandler(plugin.doHtml, plugin.doPlain),
            //…
        );
    }
}

I don't particularly like the internal references to plugin from within plugin and would probably refactor to avoid them, but this might be an easy way to get going. 我并不特别喜欢内部的引用plugin从内部plugin并有可能重构,以避免他们,但是这可能是一个简单的方法来走了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM