Agile Ajax

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 $wnd variable, 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's document object; 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.

Technorati Tags: , ,

Topics: ,

Leave a comment

Powered by WP Hashcash

About Pathfinder

  • We design and build extraordinary applications for companies looking to make the next great idea a reality.
  • learn more

Topics

WordPress

Comments about this site: info@pathf.com