在GWT jsni中使用jquery [英] Use jquery inside GWT jsni

查看:117
本文介绍了在GWT jsni中使用jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用

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

来自我的gwt jsni函数。我试过这个

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

code>

但这也不起作用。我觉得我在这里缺少语法。任何人都可以帮忙解决方案

- 你的Jsni代码看起来很好,除非你必须把它放在相应的本地函数中,并且返回一个double(或者任何其他数字类型,如果你希望gwt进行转换)。

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

如果您想通过 UncaughExceptionHandler 你应该把你的代码包装在一个 $ entry 块中

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

2-但是,我不鼓励使用jQuery,而是使用又名gQuery。所以你不必在你的.html中加载jQuery,而且你不需要在你的应用中处理jsni。

使用gQquery,你有几乎相同的jQuery语法,但是在java ,所以你有类型安全,重构,测试....但是,你也将有几十个公用程序(最简单的ajax,承诺,选择器等),这些都不在gwt核心中。



gQuery是一个轻量级的库,完全从gwt重写。它不是jQuery库的封装(就像在其他答案中说得不对),你不必在你的页面中包含jquery.js。



与任何其他gwt库,gwt编译器会摆脱你从gquery中不使用的任何东西。在你的方法中,你的应用程序必须加载所有jquery的东西。



所以在你的情况下,使用gquery将这些代码写入你的.java类:

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

3-最后,您可以选择使用纯gwt代码来获取一个元素的偏移量,如果你知道它的id:
$ b

  int top = DOM.getElementById(profileId ).getOffsetTop(); 


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

解决方案

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();

这篇关于在GWT jsni中使用jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆