Topic: Conditionals in RJS

Does RJS have the ability to generate conditionals? The following code isn't working...

if page['work_order_complete'].value == ""
  page['work_order_complete'].value = Time.now.strftime("%m/%d/%Y")
end

I thought about how mothers feed their babies with tiny little spoons and forks, so I wondered what do Chinese mothers use. Toothpicks?

Re: Conditionals in RJS

That looks ok.  What specifically is happening thats wrong?

Re: Conditionals in RJS

thabenksta wrote:

That looks ok.  What specifically is happening thats wrong?

This is the JS that it's generating:

onclick="try {
$("work_order_complete").value().== "";
$("work_order_complete").value = "11/20/2006";
} catch (e) { ... }"

You can see that the code to update the value of input box is not wrapped in an if statement, so it changes the value of input box even if a date is already entered in the box. I would expect something more like this:

onclick="try {
if ($("work_order_complete").value().== "";) {
$("work_order_complete").value = "11/20/2006";}
} catch (e) { ... }"

Another oddity is that it seems to be calling '==' as a method on the DOM object: "...value().==" (notice the dot between value() and ==)

I thought about how mothers feed their babies with tiny little spoons and forks, so I wondered what do Chinese mothers use. Toothpicks?

Re: Conditionals in RJS

The if statement in your RJS is a Ruby 'if' - it doesn't generate any JS but gets processed server-side before sending the resulting javascript back.

Re: Conditionals in RJS

could you do ?!

page << "if($('work_order_complete').value == '') {"
page['work_order_complete'].value = Time.now.strftime("%m/%d/%Y")
page << "}"