Anonymous functions with http?

Hey there,

At the moment I’m working on a small system that involves many HTTP requests that I would like to behave asynchronously.

I found that using anonymous functions /closures doesn’t work with Servoy’s HTTP plugin.

Example:

function getDataFromServiceX () {

	var HttpClient = plugins.http.createNewHttpClient();
	var PostRequest = HttpClient.createPostRequest( 'http://google.com/' );
	
	PostRequest.executeAsyncRequest( function( Response ) {
       application.output( Response.getStatusCode );
   }, function ( error ) {
       application.output( error );
   });
}

Results in:

org.mozilla.javascript.WrappedException: Wrapped java.lang.IllegalArgumentException: Method/Function is not a servoy method (C:\Path_to_file\file.js#123)

Is there some way to get this working? (I’m on Servoy 7.4, by the way).

Thanks to Marc, I now understand the concept of Servoy Methods. :)

Too bad you can’t inherit the scope of the invoking/calling function though, is there a way around this? So that you can still get access to variable X in the following example?

This doesn’t work

function getDataFromServiceX () {

   var x = '1';

   var HttpClient = plugins.http.createNewHttpClient();
   var PostRequest = HttpClient.createPostRequest( 'http://google.com/' );
   
   PostRequest.executeAsyncRequest( function( Response ) {
       application.output( Response.getStatusCode );
       application.output(x);    // x is still '1' here
   }, function ( error ) {
       application.output( error );
   });
}

// no variable x here

function getDataFromServiceX () {

   var x = '1';

   var HttpClient = plugins.http.createNewHttpClient();
   var PostRequest = HttpClient.createPostRequest( 'http://google.com/' );
   
   PostRequest.executeAsyncRequest( onRequestSuccess, onRequestError );
}

function onRequestError ( error ) {
    application.output( error );
}

function onRequestSuccess ( Response ) {
    application.output( Response.getStatusCode() );
    // no variable x here
}

Of course I could set a scope / form variable, but I’m just cautious that if the method is called twice in short succession, that variable might be overwritten by the second method…

Anyway, just curious if others have encountered something similar and whether someone has a solution for this… (Or maybe I’m seeing this all wrong :P )

You’re welcome Jesse!