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. 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.

I added a function joinMuc myself to the core, so that it can have a callback, which is not provided by the original function:

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:

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: 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? 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.

Answers


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.


You should be able to use an anonymous function that ends up calling core.onMessage with the required parameters:

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

Is this what you want?


Need Your Help

How to center without losing it's function

javascript jquery html css

I'm attempting to center my portfolio gallery. I'm having trouble centering it without losing it's function. When you click on either projects or websites it will narrow down the photos. Whenever i...