There is very little information available on the web about using jQuery within GWT; But why would anyone want to do such a thing, I don’t know that !!. Anyway, you will see how to bring the two frameworks together here; Eventhough you would not do that it should give you a fair idea about using JSNI or in general using JS frameworks within GWT.
To make something worthwhile and useful, we will be creating a very popular iPod style content flow music album navigator using jCoverflow library developed over jQuery. The interesting part is it will be invoked from GWT wrapper – JSNI.
1. First up, create a GWT project (as usual) in eclipse.
2. You would need the following libraries under the war folder : “jquery-1.3.2.js” , “jquery-ui-1.7.2.custom.js”, “jquery.jcoverflip.js”. I tested with those versions, the newer ones might also work just fine.
3. Then put the following lines of code to the html file, so that they are available for use :
From the above screenshot, note that we also need to pull up the styles from here.
Don’t forget to add this div with the images to be displayed in the coverflow as below. This is the tag on which we will be doing the manipulations:
This is all the change we need to do on the HTML file.
4. Here is the JSNI code that calls on the jquery methods. Note the way in which jQuery calls are wrapped around in the function body of the JSNI call. This is key to the magic !!
Thats it, fire up the browser you should see the coverflow in full flow. Hope it was helpful.
Image courtesy : jCoverFlip.com
Great article!
I do however have a problem and am not sure whether it is a typo. Do you have the source code i could download?
When running through gwt and eclipse I get the error:
com.google.gwt.core.client.JavaScriptException: (TypeError): $wnd.jQuery(“#flip”).jcoverflip is not a function
I would appreciate some help
regards
Peter
Project Source : https://rapidshare.com/files/1050933496/TestJSNI.rar
Hope it helps
Hi Varun,
Awesome that worked a treat. I must have a typo somewhere. I am still very new to the javascript world.
Do you have any advice on wrapping other jquery functions? I am trying to get drag and drop file uploads to work with gwt.
The two i have looked at are:
dnd-file-upload – https://github.com/pangratz/dnd-file-upload
AQDROPUP – http://plugins.jquery.com/content/aqdropup-112
Also have you used gwtquery as a way of integrating jquery since you can debug with this add on?
Once gain thanks for your help.
cheers
Peter
Hello Peter,
If you wanted to use GWT + jQuery in production environments, I suggest gwtQuery over you manually wrapping over the jQuery selectors.
Since you are already using GWT, why not try GXT ( Ext-GWT) ? – It has DND support along with good widget support. An advantage is that the framework is in pure Java. However, I am not sure if DND a custom implementation for your use case exists in GXT. But, http://dev.sencha.com/deploy/gxtdocs/com/extjs/gxt/ui/client/dnd/package-summary.html this link should get you started. Look for DragSource and DropTarget when you write your custom component.
Regards,
Varun
good artilcle..
But I am facing same error.
com.google.gwt.core.client.JavaScriptException: (TypeError): $wnd.jQuery is not a function
I can not able to download your project from given url:
Project Source : https://rapidshare.com/files/1050933496/TestJSNI.rar
so can please help me.
chears,
bhumika
I have shared the project for you : http://db.tt/wkNPzEw2 . Hope this helps you.
The above link is broken. Would you mind reposting your project?
See if this works for you. https://www.dropbox.com/sh/lnvz8gqtkm5quh1/jUYsBaKlZG
Thanks Varun, you just made my day.
Hi Varun, i tried to have the div but inside a class that extends composite, it does not work, the only way i could make it works is putting the div in the html file.
I need to put the div inside one of my class of the view.
Thanks