Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Agile Web Development With Rails, 2nd Edition (2006).pdf
Скачиваний:
30
Добавлен:
17.08.2013
Размер:
6.23 Mб
Скачать

CONCLUSION 565

end end

end end

Script.aculo.us Helpers

In addition to all the Prototype and raw JavaScript helpers, RJS also provides support for most of the functions of Script.aculo.us. By far the most common is the visual_effect method. This is a straightforward wrapper around the different visual effects supplied by Script.aculo.us. You pass in the name of the visual effect desired, the DOM id of the element to perform the effect on, and a hash containing the standard effect options.

In this example, we add a pulsate effect to the flash notice after we show it and then fade it away to remove it.

def add_todo

todo = Todo.new(params[:todo]) if todo.save

render :update do |page| page.insert_html :bottom, 'todo_list',

"<li>#{todo.name}</li>"

page.replace_html 'flash_notice', "Todo added: #{todo.name}" page.show 'flash_notice'

page.visual_effect :pulsate, 'flash_notice' page.delay(3) do

page.replace_html 'flash_notice', '' page.visual_effect :fade, 'flash_notice'

end end

end end

You can also manipulate the sort and drag-and-drop characteristics of items on your page. To create a sortable list, use the sortable method, and pass in the id of the list to be sortable and a hash of all the options you need. draggable creates an element that can be moved, and drop_receiving creates a drop target element.

23.4Conclusion

AJAX is all about making web applications feel more like interactive client applications and less like a physics white paper: it is about breaking the hegemony of the page and replacing it with the glorious new era of data. That data doesn’t have to stream back and forth on the wire as XML (no matter what Jesse James Garrett said back in February 2005). It just means that users get to interact with their data in appropriate-sized chunks, not in the arbitrary notion of a page.

Report erratum

CONCLUSION 566

Rails does a great job of integrating AJAX into the regular development flow. It is no harder to make an AJAX link than a regular one, thanks to the wonders of the helpers. What is hard, and will remain hard for a very long time, is making AJAX work efficiently and safely. So although it is great to be able to rely on the Rails helpers to hide the bulk of the JavaScript from you, it is also great to know what is actually being done on your behalf.

And remember: use AJAX to benefit your users! Your motto should be the same as a doctor’s: first, do no harm. Use AJAX where it makes your users’ lives better, not where it just confuses them or makes it harder to get things done. Follow that simple rule, and AJAX on Rails can be wonderful.

Report erratum