Thursday, April 06, 2006

RJS - Ruby JavaScript - Very cool addition to Ruby on Rails to do Javascript in Ruby:

The hardcore API documentation

RJS element and collection proxies

Rails RJS templates

From A comp.lang.ruby thread about the new features in Rails 1.1:


Here goes a few sample rjs calls:


# First buy appears the cart, subsequent buys highlight it
page[:cart].visual_effect(@cart.size == 1 ? :appear : :highlight)


# Replace the cart with a refresh rendering of the cart partial
page[:cart].replace_html :partial => "cart"


# Highlight all the DOM elements of class "product"
page.select(".product").each do |element|
element.visual_effect :highlight
end


# Call the custom JavaScript class/method AddressBook.cancel()
page.address_book.cancel


# 4 seconds after rendering, set the font-style of all company
# spans inside tds to normal
page.delay(4) do
page.select("td span.company").each do |column|
column.set_style :fontStyle => "normal"
end
end