Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call jquery trigger from gwt?

  public static native void doConnect() /*-{
            $wnd.jQuery(document).trigger('connect',
                    {
                    jid: 'sss',
                    password: 'sss'
                }

                );

        }-*/;

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

inside .js file

    $(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;
    });
like image 378
cometta Avatar asked Feb 23 '10 07:02

cometta


2 Answers

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).

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
}-*/;

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.

like image 97
Igor Klimer Avatar answered Nov 18 '22 17:11

Igor Klimer


It's a bit late for this answer, but your original code did not work due to a simple mistake: You have properly used $win instead of window but a few characters later you have used document instead of $doc :)

public static native void doConnect() /*-{
    $wnd.jQuery($doc).trigger($wnd.jQuery.Event('connect', {
        jid: 'sss',
        password: 'sss'
    }));
}-*/;
like image 4
Tomasz Gawel Avatar answered Nov 18 '22 17:11

Tomasz Gawel