Tuesday, October 21, 2008

Dojo and Rails (Prototype) - The Workaround

Imagine the surprise I felt when I realized that our pretty Dojo applications were breaking as soon as we were putting them into the Ruby on Rails project. Then it became clear, some parts of Dojo don't play well with Prototype. A Google search on Dojo+Prototype will give you muffled and possibly not applicable information.

This provides a huge dilemma:
  • We need Dojo, especially parts of dojox, for pretty interface and graphics drawing; a lot of work has already been put in.
  • We need Prototype for RJS and AJAX in an already well-established Rails project.
I know some of you purists out there think we should use one and not the other. My response to you: No.

It came to me in the shower: iframes. The fundamental problem is that we can't load both Dojo and Prototype in the same page without consequences in our Dojo graphics code. But what if we loaded an iframe that only had Dojo references into a Rails page which had Prototype loaded. It works! At least in Safari and Firefox, our main development platforms, the competing javascripts are kept separate. I haven't tested in IE.

I realize this isn't the ideal solution people want to hear. But in the end, communication between pages/iframes with separate javascript technologies is a much easier problem to deal with than getting two complex javascript technologies to function together in the same page.

UPDATE 1 (10/22/08)
Actually, I'm now an advocate of using the object tag as opposed to the iframe tag. It validates as valid xhtml 1.1, strict, with the same effect. It also sounds better than using frames.