Results 1 to 4 of 4
  1. #1
    TreyAU21 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default Possible? Callback function passed as arguments to another function

    Without getting into too much detail about what I'm doing, I'm trying to wrap a JavaScript API in Java so that I can use it for Java GWT JavaScript creation.

    None of that really matters though... so... many of the JavaScript functions are asynchronous and take in callback functions as parameters (a success callback function and a failure callback function). In order to make my JAVA API generic enough, the same type of functionality would be required.

    Can you (in Java) evaluate what's passed in as a parameter?

    Example:

    Java Code:
    public void myCallback() {
      // do something
    }
    
    public native void theWrapperFunction(String callBack) /*-{
      var i = this;
      $wnd.js_function(function({i.@com.domain.client.Class::[B][U][I]callBack[/I][/U][/B]()();});
    }-*/;

    A little explanation there (some of that is GWT specific)... but a String is passed to theWrapperFunction. That parameter would be "myCallBack"... telling this wrapper function what the callback function's name is... and then actually EVALUATING that function where bolded/italicized/underlined.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default

    Just to clarify (I have no idea about this syntax in theWrapperFunction), but does this:
    Java Code:
    $wnd.js_function(function({i.@com.domain.client.Class::callBack()();});
    actually call the method represented by the parameter callback?

    Is that how this works, or are you asking if this is how it works?

  3. #3
    TreyAU21 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Yes... the syntax is GWT (Google Web Toolkit) specific. But, the meat of my question would be "is that possible".

    To "execute" something stored in a string variable.

    For those familiar with PHP... it would be the equivalent of

    PHP Code:
    // Say that the $callback variable is passed the value "theCallBack"
    function myFunction($callback) {
      $retVal = $callback("Trey", "White");
    }
    
    function theCallBack($firstName, $lastName) {
      // Do something
    }
    In this example, myFunction($callback) will actually execute the function stored in $callback as referenced in the $retVal line. Is this similar functionality available in any capacity in Java.

    (I hate to throw this term around because I'm not sure if really applies... but I think the property of a programming language that I'm asking about is "introspection"... don't quote me on that though).

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default

    Well, as far as I can see (not knowing GWT), you're trying to do this across languages, which I can't (offhand) see working.

    If this was in Java alone, then I'd say that callbacks are usually done via an interface of some description. But this isn't likely to be the case here. Isn't there a GWT forum you could ask at?

Similar Threads

  1. function
    By nanna in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 10:20 PM
  2. Register a callback function in other class
    By barts2108 in forum New To Java
    Replies: 2
    Last Post: 11-10-2008, 05:24 PM
  3. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-08-2008, 12:33 AM
  4. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  5. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 09:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •