JSNI troubles (TypeError)

156 views
Skip to first unread message

dhoffer

unread,
Aug 22, 2012, 4:36:11 PM8/22/12
to Google Web Toolkit
I'm trying to write a little JNSI but am having trouble, I get an
exception with this this message (TypeError): Object expected

When I run this code:

public static native void init(String id) /*-{

var elem = $doc.getElementById(id);
addEvent(elem, 'drop', droppedHandler);
addEvent(elem, 'dragover', allowDrop);

function allowDrop(event) {
if (event.stopPropagation)event.stopPropagation();
if (event.preventDefault)event.preventDefault();
return false;
}

function droppedHandler(event) {
if (event.preventDefault)event.preventDefault();
return false;
}

var addEvent = (function () {
if (document.addEventListener) {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.addEventListener(type, fn, false);
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
} else {
return function (el, type, fn) {
if (el && el.nodeName || el === window) {
el.attachEvent('on' + type, function () {
return fn.call(el, window.event);
});
} else if (el && el.length) {
for (var i = 0; i < el.length; i++) {
addEvent(el[i], type, fn);
}
}
};
}
})();
}-*/;

Where in my extended RichTextArea I set a unique ID on its element
via:

private final String id = UUID.uuid();
getElement().setId(id);

I don't think the problem is in the body of the method...i think there
is something wrong with getting a valid Element to the addEvent()
method. Also is there a way to debug this somehow so I can see what
is really happening?

Thanks,
-Dave

Joseph Lust

unread,
Aug 22, 2012, 5:44:54 PM8/22/12
to google-we...@googlegroups.com
Generally it is an antipattern to reference by id in GWT due to how UiBinder works behind the scenes with Id's. Can you pass an element into the JSNI method on which to operate?


Sincerely,
Joseph

dhoffer

unread,
Aug 22, 2012, 7:54:09 PM8/22/12
to Google Web Toolkit
I tried that too and got the same error.

I was given the javascript code that started like this (where dropBox
was the id of the div)

var dropBox = document.getElementById("dropBox");
addEvent(dropBox, 'drop', droppedHandler);
addEvent(dropBox, 'dragover', allowDrop);

And I was asked to add this to our GWT app where the widget is now a
RichTextArea. Initially I used getElement() and passed that directly
into the init method so the JSNI method used the passed elem
directly...but that gave the same exception. I just don't understand
why.

Thanks,
-Dave

Alfredo Quiroga-Villamil

unread,
Aug 22, 2012, 10:07:55 PM8/22/12
to google-we...@googlegroups.com
Decorate your JSNI with some "console.log(...." statements. Open up in
your browser your Dev Tools (depends on the browser you are using),
look at the console and narrow down the issue there.

If you provide a simple concise example that allows the issue to be
easily duplicated, I'll be happy to take a look at it here as well.

Best regards,

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



--
Alfredo Quiroga-Villamil

AOL/Yahoo/Gmail/MSN IM: lawwton

Thomas Broyer

unread,
Aug 23, 2012, 4:59:15 AM8/23/12
to google-we...@googlegroups.com
How about converting the code to Java/GWT?

widget.addDomHandler(new DropHandler() {
    @Override
    public void onDrop(DropEvent event) {
        event.stopPropagation();
        event.preventDefault();
    }
}, DropEvent.getType());
widget.addDomHandler(new DragOverHandler() {
    @Override
    public void onDragOver(DragOverEvent event) {
       event.preventDefault();
    }
}, DragOverEvent.getType());

dhoffer

unread,
Aug 23, 2012, 12:50:42 PM8/23/12
to google-we...@googlegroups.com
Thanks for the replies.  With the console logging I was able to figure out why it was failing, it's working now.

But I also like the idea to converting to Java/GWT...I'll try that too.

Thanks!
-Dave
Reply all
Reply to author
Forward
0 new messages