- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
GWT with JSONP
I've been looking to add to my JSONP series with some examples using GWT. Dan Morrill over at Google has beaten me to the punch with an article on just this subject. The code for it is rather straightforward, but there are a few sticky bits that make it a little less so:
I spent quite a while debugging this, until I finally asked Scott Blum, a GWT Engineer. Scott merely asked a question: "Was the array created in the same window in which you're testing it?"
What Scott knew that I did not, is that the JavaScript classes (like Array) corresponding to the primitive types are constructed along with the window object. Because JavaScript is a prototype-oriented language, the "classes" are really just object instances with special names. These two issues combine to reveal a subtle but important issue: the Array objects from two different windows are not the same object! The expression "x instanceof y" in JavaScript boils down to something like this pseudocode: "if the 'prototype' property of 'x' is the same object as 'y', return true, else return false".
At this point, you may be wondering how multiple windows entered the discussion. The key is the
$wndvariable, which points to the hidden iframe in which the application's GWT code is loaded. The DOM object in GWT, however, points to the parent window'sdocumentobject; after all, your application code is interested in manipulating the browser window, not GWT's hidden iframe. As a result, in the code above, the<script>tag is added to the parent window, while the code using it resides in a different iframe. This means that the object is created in a window different from where the "instanceof" checks are made, thus causing the issue above.
The solution Dan chose was to add the <script> tag to the iframe that contains the GWT code.
Of course the new "cross site" compilation option produces code that loads directly into the main page, so this hack may not in fact be necessary.
Comments: 1 so far
Leave a comment
About Pathfinder
Recent
- Firefox Plugin Malware ‘Trojan.PWS.ChromeInject.A’
- Pathfinder releases version 1 of the its Flash Platform microsite (codename Mica)
- Pimp my Rails: Five Plugins & Gems to Make Rails Better
- iPhone: Using Pre-processor Directives for Device Testing
- Subtle OpenGL Projection Matrix Difference Between iPhone Simulator and Device
- App Security: Throw Out the Org Chart!
- Pimp my jQuery: Five plugins to replace the features Prototype and Scriptaculous users expect
- Thanksgiving 2008: What We’re Thankful For (In Rails)
- iPhone SDK: Testing with TextMate & GTM
- GWTQuery - JQuery-like Syntax in GWT
Archives
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006


HI there
Thank you for this insight. Just to confirm I have checked and you are correct if you compile in XS mode you no longer have to add the script directly to the iFrame.
In fact if you add it as normal ($wnd[callback]) this ensures you can maintain session state on the server side otherwise you cannot.
Thanks for the advice. This help me solve a problem I was having with session state.
Eggsy
Comment by Eggsy, Wednesday, October 22, 2008 @ 3:15 am