Reputation: 85
I am new Google web toolkit.I am confused about JSNI.Why and when use of JSNI in Google web toolkit.Advantage and limitation of JSNI.Thanks in advance.
Upvotes: 1
Views: 175
Reputation: 3832
Beside the points alexp already mentioned, there is another situation, where JSNI is a very useful.
Imaging, you have several GWT applications and in case you left one and start another, you want to send some informations to the starting GWT application.
You have several possibilities to do that:
you can use URL parameters
you can use a cookie
or use an JSP and JSNI.
There fore, you use a JSP instead of a HTML as your host page. Inside this JSP you can use a JSP tag of a hidden field to carry your parameter and with JSNI you can read the parameter out of your host page.
Upvotes: 0
Reputation: 797
I use native Javascript code when there is no other way of coding the feature other than in Javascript. For example, there is no wrapper for XmlHttpRequest object in GWT. I could use either third-party solution if it exists, or write my own classes based on javascript-based sample code found around on the web. So, I have this native method that I can call from my pure Java code and that contains only Javascript code. JSNI allows me to share the data between these two worlds.
And after all, after permutations are compiled, both Java and JS native methods are build into Javascript.
If you want to use various Javascript libraries, then JSNI will let you to call methods as designed wrapped into your Java methods. Again, for example, you can include Modernizr.js script into your html page and then have something like this in your class:
private native boolean isCSSAnimationSupported() /*-{
return Modernizr.cssanimations;
}-*/;
I do not use native methods when there is no actual need to inject inline Javascript.
Upvotes: 2