Child pages
  • Alternative Technologies-Ruby on Rails

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Pierce T. Wetter III

Myth: Rails is a better framework for Ajax than WO.

Reality: WebObjects is actually a better framework for use with Ajax libraries than Rails because it has a better component system than Rails. You spend a lot of time coding little tiny XML and HTML generators when doing Ajax and WO's component system makes that very DRY (Don't Repeat Yourself).

Myth: WebObjects is hard to use with Ajax.

Reality: WebObjects is easy to use with Ajax, it's just that there is only one known library for Ajax-WO support, and its not well documented. Even then the library only goes so far in that it just provides new components to wrap a few script.aculo.us tags. I think its more of a documentation gap, not a code gap.

Additionally, the WO documentation has always pushed people erroneously towards component actions and away from direct actions. If you use 90% direct actions like I do in my application, (See my "WebObjects on Rails" post), you'll find that direct actions are simple to code in reality, and very much like Rails which basically doesn't have component actions at all.

Which is to say, if you avoid much of the power of WO, and don't use component actions, ajax will be easier. If you do use component actions - and I have yet to work on a project that doesn't - then ajax use seems like it will blow your page cache, as described below. So it really is easy to use AJAX in WO. Really easy. It's just that it doesn't work.

[mschrag: While I agree with the above commenter that component actions provide a huge amount of power in WO, it is NOT true that Ajax and component actions are incompatible. Project Wonder's Ajax components directly addresses the use of component actions with Ajax in a way that does NOT blow the page cache. While it is true that the implementation of these capabilities inside of Wonder was non-trivial, it demonstrates that it is, in fact, possible, and if you use the PW components along with ERXSession, you will get this capability for free.]

Myth: You can't assign the name or id value of tags in WebObjects.

I'm mentioning this one explicitly, because I had someone ask me about this recently, because they were trying to assign a WYSIWYG JavaScript editor to a text area.

Reality: Actually, what really happens is that if you DON'T have a name=value or id=value line for your TextArea in WebObjects, it will generate a unique one for you. But you're more than welcome to specify one in your .wod file, and WO will use that. It's then up to you to make sure its unique, so that you don't have multiple text area tags with the same name. In other words if you only have one textarea you wish to attach a Javascript WYSIWYG editor to, just add:

Panel
Wiki Markup
Code Block

textarea: WOText

{

  id='wysiwyg';

  name='wysiwyg';
  
value=textValue;

}

in your .wod file and you can use id with your Javascript just fine.

Myth: WebObjects generates HTML

Reality: Just because the components of your .wo file are .html and .wod doesn't mean WebObjects can only generate HTML. WebObjects is really at heart, a very, very, very, complicated printf. The result of a component can be HTML, XML, JavaScript, or even binary data. You can actually put whatever you want in the .HTML and treat WebObjects like a giant merge engine. For instance, you could generate PDF files using WebObjects; they're just text, and you could substitute text into the middle of the PDF boilerplate pretty easily.

Background

We haven't used a lot of JavaScript at www.marketocracy.com, for a very simple reason: We don't have the testing staff to fire up 10 different browser variations to test the site. And lets face it, programming in JavaScript sucks.

Since someone invented that cool acronym though, that's changing. You would think it shouldn't matter that something has a name, but it does. I remember when the GoF Design Patterns book came out. There was nothing in there I hadn't figured out on my own, but now they had a name! _ I _ could _ say _ "Singleton" _ to _ my _ co-workers _ and _ they _ would _ know _ what _ I _ was _ talking _ about. _ I _ could _ buy _ a _ junior _ engineer _ the _ book, _ and _ he _ would _ soon _ be _ programming _ at _ a _ much _ higher _ level.

So _ having _ a _ name _ helped, _ and _ the _ result _ is _ that _ there _ are _ a _ lot _ of _ cool _ toolkits _ out _ there. _ Since _ someone _ else _ wrote _ the _ JavaScript, _ that _ means _ I _ don't _ need _ to _ test _ 10 _ different _ browser _ variations.

Which _ means _ I _ can _ make _ the _ site _ much _ easier _ to _ use _ and _ more _ interactive. _ Huzzah!

Research

So the first thing I did was that I'd heard that Rails was cool and made Ajax easy. I'd heard that before, but that was when the version number was 0.13... So I went out and bought the Rails books and you know what I found?

It's not that Rails rocks with Ajax, its that the Prototype Javascript library rocks. The documentation makes a big deal about doing Ajax with one line of code.

panel
Code Block
xml
xml

<div id='<= posting.id'>


  <%= link_to_remote  [div to update] [link options] -> %>


</div>

The reality is that all Rails is doing is writing one line of JavaScript. From the Ajaxy "rating" code I've been adding to my app, look at the following:

Panel
Wiki Markup
Code Block
xml
xml

<div id='<WEBOBJECT name=postingID></WEBBOBJECT>'>

  
<a href="#" onclick="new Ajax.Updater('<WEBOBJECT name=postingID></WEBBOBJECT>',
       '<WEBOBJECT name=ratePosting></WEBOBJECT>', {asynchronous:true, evalScripts:true}); return false;">1</a>

</div>

Ok, so lets break it down. The way Ajax.Updater works from the Prototype library is you give it the id of a DOM object and a URL, and it replaces the DOM object with that id with the contents of the URL. Usually, you specify the area to update with a div tag which I've shown for completeness. For ratings I would need 1 div tag to enclose all 5 rating stars: <div><a></a><a></a><a></a><a></a><a></a></div>. I would also use a WOGenericContainer to generate the div tag with the right id rather than using the id='<WEBOBJECT name=postingID></WEBBOBJECT>' line, but I wanted it to be obvious it was div tag.

...

Here I have a manually built <a> tag with an onclick bit of javascript. That makes a single call out to the Prototype library, which has this cool call:

Panelcode

new Ajax.Updater( elementid, url, options)

It does something very simple: It pulls the HTML from the specified URL, and replaces the element with the specified id with the downloaded HTML.

The two WebObjects tags specify the element ID and the link, they're just a WOString and a WOActionURL:

Panel
Wiki Markup
Code Block

postingID: WOString { currentPosting.primaryKeyString; }

ratePosting: WOActionURL { see discussion }

Now in my case, I'm a direct action snob. So my WOActionURL would look like the following:

Panel
Wiki Markup
Code Block

ratePosting :WOActionURL

{

  directActionName="PostingRater";

  ?pkey=currentPosting.primaryKey;

  ?rating=cRating;
  
?wosid=NO;

}

This produces a result similar to Rails, because in rails you have to define an action for each class of link. In my case I tie directactions to pages/components, so my "PostingRater" page would return component-level HTML (minus any HEAD/BODY tags) that matched the existing <div> definition. Since we're using WebObjects, that turns out to be trivially easy if we build the enclosing <div> tag with a component PostingRater can just look like:

panel
Code Block
xml
xml

<WEBOBJECT name=RatingDiv></WEBOBJECT>

Using component actions, it could be even simpler:

Panel
Wiki Markup
Code Block

ratePosting: WOActionURL { action=ratePosting;}

Because WebObjects, unlike Rails can have stateful components, the RatingDiv component could actually have all the logic and return self as the result of the action:

Panel
Wiki Markup
Code Block

public WOReponse ratePosting

{

  currentPosting.ratePosting(currentRating);

  return self; // since this is called from JavaScript

               // return just myself, not self.page

               // this will tell WO to render only

               // this as a result.

}

That is, the link for the javascript would go into the RatingDiv component with everything already setup: the current posting, the current rating. Returning self then causes the div to regenerate.

...

I also think, that from this example, dojo is a bit weak as a Javascript toolkit. You'll spend a lot of time wiring stuff up in Javascript with Dojo compared to Prototype. Or contrast dojo with the Yahoo UI library:

http://developer.yahoo.com/yui/Image Removed

The whole "Dialog" thing in yui is cool:

http://developer.yahoo.com/yui/container/dialog/index.htmlImage Removed

And would lend itself quite readily to the model WO has of building pages out of pieces.

...

So exactly what problems are you having with dojo/wo? Consider that the problem may be dojo, not WO in particular. You could easily create a WODynamicElement for every new dojo tag, collect them in components, and have something much easier to use than straight dojo.Category:WebObjects