RJS与ActionView

ActionView为我们封装RJS代码到update_page()块里面,比如thought_log的例子会生成如下代码:
[code]
update_page do |page|
page.insert_html :bottom, 'thoughts', :partial => 'thought'
page.visual_effect :highlight, 'thoughts'
page.form.reset 'thought-form'
end
[/code]

当partial中需要变量时我们可以这样做:
[code]
page.replace_html 'user', :partial => 'user',
:locals => { :name => 'Cody Fauser',
:title => 'EL Presidente'}
[/code]

RJS中的page实际为一个JavaScriptGenerator对象,它可以做Element/Class/Collection的Proxies
Element Proxies:
[code]
page['header'].hide
page[:header].hide
page[:header].hide.show
[/code]

Class Proxies:
我们在public/javascripts/application.js里添加一个JavaScript类
[code]
var Alerter = {
displayMessage: function(text) {
alert(text);
}
}
[/code]
然后我们可以这样调用Alerter类
[code]
page.alerter.display_message('Welcome')
[/code]

Collection Proxies
[code]
page.select('#content p')

page.select('#content p').each do |element|
element.hide
end

page.select('#form input[type=text]').each do |element|
element.hide
end
[/code]

页面上的helper方法
[code]
link_to_remote(name, options = {}, html_options = {})
[/code]
This is most common way to make an Ajax call with Rails. link_to_remote() is a helper that generates a hyperlink that makes an Ajax request when clicked. Rails generates either an Ajax.Request or an Ajax.Updater in the onclick() event of the <a> tag, depending on whether or not the :update option was passed to link_to_remote(). For the purposes of RJS, the :update option should not be used because the Prototype Ajax.Updater object expects an HTML response and RJS returns a JavaScript response. If you are having any weird problems with parts of your RJS response appearing on your page, then you're probably using the :update option with an RJS template.

[code]
link_to_function(name, function, html_options = {})
[/code]
Generates a hyperlink that executes a JavaScript function or code when clicked. This doesn't actually create an Ajax request, but it can be used to execute custom JavaScript functions that do. Use this method to call your custom JavaScript libraries that use Ajax.Request to make the Ajax calls.

[code]
abserve_field(field_id, options = {})
[/code]
Observes a field and makes an Ajax request when the content has changed or the event specified with :on has occurred

[code]
remote_function(options)
[/code]
Generates the JavaScript to make a background Ajax request to a remote controller action. This method is useful for making remote Ajax calls from the event handlers of DOM objects, such as the onchange() event of a <select> tag. Takes the same options as link_to_remote().

[code]
observe_form(form_id, options = {})
[/code]
Works just like observe_field(), but observes an entire form.

[code]
form_remote_tag(options = {})
[/code]
Creates a form tag that submits the contents of the form using a background Ajax request. This is another very common way to make Ajax requests.

[code]
form_remote_for(object_name, object, options = {}, &proc)
[/code]
Just like form_remote_tag(), but uses the form_for semantics introduced in Rails 1.1.

[code]
submit_to_remote(name, value, options = {})
[/code]
Creates a button that will submit the contents of the parent form to the remote controller action. The options are the same as for form_remote_tag().

[code]
in_place_editor_field(object, method, tag_options = {}, in_place_editor_options = {})
[/code]
Makes an Ajax request when changes to the field are committed. To use this method with RJS you have to pass in the option :script => true to the in_place_editor_options hash.

[code]
in_place_editor(field_id, options = {})
[/code]
This is the method that the in_place_editor_field() wraps. To use this method with RJS you need to pass in the :script => true option to the options hash.

[code]
drop_receiving_element(element_id, options = {})
[/code]
Makes an Ajax request when droppable elements are dropped onto the element.

[code]
sortable_element(element_id, options = {})
[/code]
An Ajax request is made whenever this element is sorted using drag and drop.

[code]
Ajax.Request(url, options)
[/code]
All Rails helpers use this Prototype object to make the actual Ajax requests. You can also use this object to make remote Ajax requests from your JavaScript libraries. This is a JavaScript object and not a Ruby object.

我们还可以写helper方法给RJS模板使用,例如:
[code]
def insert_item(list_id, item)
page.insert_html :bottom, list_id, '<li>#{item.title}</li>'
page.visual_effect :highlight, 'item #{item.id}', :duration => 0.5
end
[/code]
然后我们就可以在RJS模板中这样使用了:
[code]
page.insert_item 'my_list', @item
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值