On 8/10/2011 4:50 PM, e-lena-s wrote:
I am trying to listen to a custom JS event that is created outside my
GWT module. In my JS file I create the event, and then fire it as
such :

        var evt = document.createEvent("Event");
        evt.initEvent('customEvent', true, true);
        evt.customData = "someData";

        window.addEventListener('customEvent', function(evt)
{ console.log("test: " + evt.customData)}, false);
        window.dispatchEvent(evt);

Now, in my GWT code, I have a JSNI method that adds a listener  to
listen to my customEvent  and act on it. Code as follows:

public native void addLookupResultHandler()/*-{
         $wnd.addEventListener('lookupEntitySelected',
$entry(function(evt){

console.log("gwt: " + evt.customData);

console.log("gwt: " + evt.type);

@myClassName::handleEvent(LmyCustomEventEvent;)(evt)
                                                        }), false);
     }-*/;


The problem I have is that the customData is being dropped when the
event gets to JSNI code. I can see that the event listner written in
JS does get the correct event with the correct customData, but logging
the event properties in JSNI shows that customData is undefined (event
type looks correct though)

Am I doing something wrong here ?

Is there may be a better way to create custom events (it has to be
created in JavaScript, since the code firing it won't be in GWT
module)

Probably make your JSNI much lighter in that it should just pass arguments to a GWT routine that actually fires the event. The docs are very good on this account.

Believe it or not, but your JS example is doing too much work. Unless you're porting existing code, in which case you're actually just writing wrappers, there should be a good reason to write a JSNI routine that's more than one or two lines. This example does not clear that bar.

--
You received this message because you are subscribed to the Google Groups "Google 
Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to