Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jquery inside GWT jsni

I am not able to use

$("#"+profileId).offset().top

from inside my gwt jsni function. I tried this

$wnd.$("#"+profileId).offset().top

but this is also not working. I feel i am missing syntax here. Could anybody help

like image 290
javalearner Avatar asked Apr 16 '13 06:04

javalearner


People also ask

What is GWT jsni and how to use it?

GWT JSNI is used to solve problems such as, when we need to integrate GWT with existing handwritten JavaScript or with a third-party JavaScript library. Occasionally, we need to access low-level browser functionality which is not exposed by the GWT class API’s.

Can I use jsni in JavaScript?

JSNI is web equivalent of inline assembly code and can use in many ways such as: Implement a Java method directly in JavaScript. Wrap type-safe Java method signatures around existing JavaScript. Call from JavaScript code into Java code and vice-versa. Throw exceptions across Java/JavaScript boundaries.

What is the difference between calling jsni from Java and native methods?

The caller can’t really tell if the method is native or not. This gives some flexibility in changing about how the method is implemented. It is more complex than calling JSNI from Java. For example, suppose we pass an object to a JSNI method and need to access a field or call a method in that object.

What is the difference between jsni and JNI method signatures?

JSNI method signatures are exactly the same as JNI method signatures except that the method return type is left off. The following table shows type signatures: It demonstrates passing numbers, strings, booleans, and Java objects into JavaScript.


1 Answers

Three solutions for this question:

1- Your Jsni code looks fine except that you have to enclose it in the corresponding native function and return a double (or any other number type if you want gwt to make the casting).

 native double getTop(String profileId) /*-{
   return $wnd.$("#" + profileId).offset().top;
 }-*/;

If you wanted to see errors through your UncaughExceptionHandler you should wrap your code in a $entry block

native  double getTop(String profileId) /*-{
  return $entry(function(data) {
    return $wnd.$("#" + profileId).offset().top;
  });
}-*/;

2- But, instead of using jQuery, I encourage you to use gwt-query aka gQuery. So you dont have to load jQuery in your .html and yo dont need to deal with jsni in your app.

With gQquery you have almost the same jQuery syntax but in java, so you have type safe, refactoring, testing .... But also, you will have dozens of utilities (simplest ajax, promises, selectors, etc) which are not in the gwt core.

gQuery is a light-weight library, fully rewritten from scratch in gwt. It is NOT a wrapper of the jQuery library (like is incorrectly said in the other answer), you dont have to include jquery.js in your pages.

Like with any other gwt library, the gwt compiler would get rid of anything you dont use from gquery. In your approach, your app has to load all the jquery stuff.

So in your case, and using gquery write this code in your .java classes:

import static com.google.gwt.query.client.GQuery.*;
... onModuleLoad() {
   int top = $("#"+profileId).offset().top;
}

3- Finally, you have the option of using pure gwt code to get the offset of an element, if you know its id:

int top = DOM.getElementById(profileId).getOffsetTop();
like image 183
Manolo Carrasco Moñino Avatar answered Oct 06 '22 00:10

Manolo Carrasco Moñino