Create JavaScript callback in GWT using JSInterop

366 views
Skip to first unread message

Tony

unread,
Sep 10, 2016, 11:01:25 AM9/10/16
to GWT Users
hi

In JavaScript, in order to get the login status using the Facebook SDK, one has to call the FB.getLoginStatus like so:

FB.getLoginStatus(function(response) {
  if (response.status === 'connected')
      // DO SOMETHING
   
 });


notice the function(response) { ... } that is passed as a parameter to getLoginStatus.

I am able to map the FB object in GWT with the proper JSMethods like so:

@JsType(isNative = true)

public static interface FB {

  void init(FBParamObj fbParamObj);

  void getLoginStatus(CustomCallbackFunction callbackFunction);

}


However, I'm not sure how to define the CustomCallbackFunction callbackFunction that I need to provide as a parameter to FB.getLoginStatus. FB.getLoginStatus is provided by the Facebook SDK so I can not change it. 


How do I create this JavaScript callback function in GWT so I can pass it to the FB SDK as a parameter? 

I'm sure I'm missing something very basic but I can't seem to find it in the documentation. 




Thank you for the help!

Tony

zakaria amine

unread,
Sep 10, 2016, 11:28:05 AM9/10/16
to GWT Users
Hello, 

You need to use @JsFunction, something like that would do the trick in your case: 

@JsFunction
public interface Function{
public JavaScriptObject call(FBResponse event);

}

and then you can define your FBResponse either using JsInterop or JSNI: 

@JsType(isNative=true, namespace=GLOBAL, name="Object")
public interface FBResponse{
public int status;

}
Message has been deleted

Tony

unread,
Sep 10, 2016, 2:50:51 PM9/10/16
to GWT Users
Thank you Zakaria! I had everything except the @JsFunction interface. Here's the full code in case anybody else needs it: 


@JsType(isNative = true)

private static class FbResponse {

       private String status;

       private FbAuthResponse authResponse;

}


@JsType(isNative = true)

private static class FbAuthResponse {

       private String accessToken;

       private Integer expiresIn;

       private String userID;

}


@JsType(isNative = true)

private static interface FB {

       void init(FBParamObj fbParamObj);

       void getLoginStatus(FbLoginStatusCallbackJsFunction fbLoginStatusCallbackJsFunction);

}


@JsProperty(namespace = JsPackage.GLOBAL)

private static native FB getFB();


private static class FBParamObj {

       @JsProperty

       String appId = ApplicationClientConstants.FacebookAppID;

       @JsProperty

       String version = ApplicationClientConstants.FacebookAuthSDKVer;

       @JsProperty

       boolean cookie = true;

       @JsProperty

       boolean xfbml = false;

}


@JsMethod(namespace = JsPackage.GLOBAL)

public static final void fbAsyncInit() {

       getFB().init(new FBParamObj());

       getFB().getLoginStatus(new FbLoginStatusCallbackJsFunction() {

               @Override

               public JavaScriptObject call(FbResponse response) {

                       Window.alert(" status: " + response.status + //

                       "\n userID: " + response.authResponse.userID + //

                       "\n accessToken: " + response.authResponse.accessToken + //

                        "\n expiresIn: " + response.authResponse.expiresIn);

                       return null;

               }

       });

}


@JsFunction

private interface FbLoginStatusCallbackJsFunction {

       public JavaScriptObject call(FbResponse response);

}

Reply all
Reply to author
Forward
0 new messages