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
Follow the Blog
-
Get a monthly update on best practices for delivering successful software.
Subscribe via email
Subscribe via RSS
Categories
Topics
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- 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
Blogroll
Recent
- Elements of Testing Style
- Aesthetics and Web Design
- Asterisk-Java Testing with Groovy
- 3 Misuses of Code Comments
- Fluently NHibernate
- Digging a Hole and Covering it with Leaves — The Software Development Version
- The Importance of User Experience - Do You Understand It in Your Bones?
- Writing Your Own Protocol With NSURLProtocol
- What’s In Your Dock: iPhone edition
- Feature Fatigue

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