passing a callback function to a module

I wanted to create a shared library to do some communications to an external web server, that I want to use from various apps. I have created a monkey barrel but have not been able to pass a callback function from my main application to the monkeybarrel module, the problem is that there is a callback within the module itself (to handle the asynchronous HTTP repose)

//in my main app:


//call to the 'login function' in my monkeybarrel module
testBarrel.comms.login(URL, uname, pwd, method(:my_callback_function));

// the callback function in my main app to handle the response data from the login 
function my_callback_function(data) {
    // do stuff with data
}


//and in my tesBarrel.comms module

(:comms)
module comms {
    //local vars
    var _loginCallback;
    
    
    function login(URL, uname, pwd, callback) {
        // create headers, params and options here...
        
        //as this is inside a module, I had to do this...
        var callable = new Lang.Method($, :login_response);
    	
    	//make the web request
    	Comms.makeWebRequest(loginURL, params, options, callable);
    }
	
	function login_response(responseCode, data) {
	    // do some processing here
	    // then invoke the callback function from the main app to pass the result over to it
	    _loginCallback.invoke(responseCode);
    }
}
 

I can see that the webrequest is sent, but It does not seem like the login_response function ever gets invoked. 

When I run the main app I get this:

Error: Symbol Not Found Error
Details: Failed invoking <symbol>
Stack:

Again, it seems like it fires off the web request (can be seen in the HTTP traffic log) but I never see response in the HTTP log, which is odd. Except for the callback function, I use the exact same code Ive used directly fro my app so I know that the web request is good.  

If I manage to get the login_response callback to work, I guess I have the next problem with invoking the "original" callback function from the main app...   

  • I don't see where you are setting _loginCallback;, yet you are using it in login_response.

  • I agree with . It looks like you want this...

    function login(URL, uname, pwd, callback) {
        // create headers, params and options here...
            
        //as this is inside a module, I had to do this...
        var callable = new Lang.Method($, :login_response);
        
        _loginCallback = callback;
        	
        //make the web request
        Comms.makeWebRequest(loginURL, params, options, callable);
    }

  • Thank you for responding, I seem to keep you busy with all my questions. Sorry for that. 

    I accidentally left out this line in my explanation above. in the login function, I do the following:

    function login(URL, uname, pwd, callback) {
            
            _loginCallback = new Lang.Method($, :callback);
            
            // create headers, params and options here...
            
            //as this is inside a module, I had to do this...
            var callable = new Lang.Method($, :login_response);
        	
        	//make the web request
        	Comms.makeWebRequest(loginURL, params, options, callable);
        }
    

    I get a proper response back from the web server (I didn't see it previously in the HTTP traffic log but it was there), but the login_response function never gets invoked.  

    So seem to be something else that I'm missing. 

  • *** SOLVED ***

    When I changed this:

    Comms.makeWebRequest(loginURL, params, options, new Lang.Method($, :login_response));

    to this:

    Comms.makeWebRequest(loginURL, params, options, new Lang.Method(self, :login_response));

    then it worked!

  • Hmmm. those two lines should do exactly the same thing in this context. That said, you should probably consider putting your code into a module and classes.