Child pages
  • Alternative Technologies-Ruby on Rails

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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:

Code Block
  textarea: WOText
 
{
    id='wysiwyg';
    name='wysiwyg';
    value=textValue;
 
}

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

...

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.

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:

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:

Code Block

 
new Ajax.Updater( elementid, url, options)

...

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

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:

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:

Code Block
xml
xml


 <WEBOBJECT name=RatingDiv></WEBOBJECT>

Using component actions, it could be even simpler:

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:

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.

...