如何从gwt调用jquery触发器? [英] How to call jquery trigger from gwt?
问题描述
public static native void doConnect() /*-{
$wnd.jQuery(document).trigger('connect',
{
jid: 'sss',
password: 'sss'
}
);
}-*/;
我试过上面的,但是在firebug或gwt托管模式下没有错误
console (所以我不知道代码是否成功)。我可能知道这是调用jquery触发器的正确方法吗?但是当我把alert()放在bind('connect')中时,它并没有在.js文件中被称为 b
$ b
i tried the above ,but there is no error in firebug or gwt hosted mode console(so i cannot know whether the code is success or not). may i know is this the correct way to call jquery trigger? but when i put alert() in bind('connect'), it was not called
$(document).bind('connect', function (ev, data) {
alert('not call.....at all');
var conn = new Strophe.Connection(
"http://bosh/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
}
});
Hello.connection = conn;
});
推荐答案
在控制台/开发模式下没有错误,但代码没有像我想要的那样行为。原因是jQuery(和这样的框架)扩展/改变了JavaScript的许多核心元素,并期望它保持这种方式 - 然而,GWT代码(也就是JSNI的东西)是从一个干净的iframe执行的(所以不需要外部框架可能会混淆语言并在GWT中导致一些奇怪的错误,这就是为什么您必须通过 $ wnd
)引用主窗口。
I had similar issues when using jQuery UI with GWT - no errors in console/dev mode, yet the code did not behave like I wanted. The reason was that jQuery (and such frameworks) extend/change many core elements of JavaScript and expect it to stay that way - however, GWT code (meaning, also JSNI stuff) is executed from a "clean" iframe (so that no external frameworks can mess with the language and cause some weird errors in GWT, that's why you have to reference to the main window via $wnd
).
我建议将您的 doConnect
函数移动到主机页面(或者链接到主机页面的外部js文件),而不是从该函数调用你的JSNI存根:
I'd suggest moving your doConnect
function to the host page (or external js file linked to the host page) and instead just call that function from your JSNI stub:
public static native void doConnect() /*-{
$wnd._doConnect('sss','sss'); //_doConnect defined in the host page
}-*/;
或者提供辅助函数,它们将从主机页面返回数组等等,以便它们包含所有jQuery所做的和期望的更改。
Or provide helper functions that will return Arrays, etc, from the host page, so that they include all the changes that jQuery made and expects.
这篇关于如何从gwt调用jquery触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!