Saturday, September 10, 2016

Re: Create JavaScript callback in GWT using JSInterop

Thank you! I had everything except the @JsFunction. Here's the final 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);




        }







On Saturday, September 10, 2016 at 11:28:05 AM UTC-4, zakaria amine wrote:
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;

}



Le samedi 10 septembre 2016 17:01:25 UTC+2, Tony a écrit :
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

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment