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]

RJS与ActionView相关推荐

  1. Debugging RJS

    RJS使得我们可以很容易的实现AJAX效果,但是bug还是会发生. 在这一节,我们展示如何在客户端和服务器端调试RJS错误. 上一节中,我们演示了让用户可以通过AJAX添加reviews,但是这个功能 ...

  2. 诗歌rials 之RJS的tips

    一些RJS的tips ruby代码 # do_magic.rjs page[:reviews].toggle page[:review_name].value = "this is cool ...

  3. rails3 新特性 和 RJS评论

    rails 3.0是2010年8月份发布的.迄今为止,3.0已历经多个tiny版到了3.0.8.3.1已经放出rc4,看起来离正式版已为期不远.相对于2系,3系还是有一些令人惊喜的变化,而且在架构上也 ...

  4. RJS教程 -入门介绍

    http://www.railscn.com/viewtopic.php?t=1065 亮凉的良帖子, 自认为 http://www.hagus.net/ror-rjs-tutorial [img]h ...

  5. requireJS,rjs,gulp简易实现

    gulpfile.js var gulp = require("gulp"); var rjs = require("requirejs"); gulp.tas ...

  6. RJS Debugging

    1,在开发模式下,默认environments/development.rb里的config.action_view.debug_rjs为true,RJS调用会被JavaScript的try/catc ...

  7. rails中使用rjs

    先来一段rjs的说明: 它是一个文件,保存在app/views 目录下,扩展名为.rjs ,其中包含的命令可以生成JavaScript代码,并交给浏览器去执行. 模板本身的用法和.html.erb 模 ...

  8. RJS Reference

    [size=xx-large][color=indigo]JavaScriptGenerator[/color][/size] 摘自:<OReilly.RJS.Templates.for.Rai ...

  9. 用RJS写的检测用户名和email是否存在

    1, Client的check url <%=link_to_remote 'Check Availability',   :submit   => "signupForm&qu ...

最新文章

  1. XSD标准架构-----xsd:element 元素详解
  2. ASP防止SQL注入-代码片段
  3. php跨域资源共享,CORS 跨域资源共享
  4. Python GUI Programming (Tkinter)
  5. MySQL 常见的开放性问题
  6. npm包管理器安装模块
  7. JavaScript学习总结(3)——JavaScript函数(function)
  8. element table多选表格_【经验总结】vue + element-ui 踩坑—— table 篇
  9. 微信小程序 java运动健身课程打卡系统uniapp
  10. 基于Java开发的学校信息管理系统的设计与实现(含论文及毕业设计源码、数据库文件)
  11. gb和gib的区别_内存 G和GB有什么区别
  12. mysql异常-SQLSTATE[HY000]: General error: 1436 Thread stack overrun
  13. vue + element-ui本地下载图片
  14. 门禁卡怎么弄到手机上,手机变成门禁卡,手把手超详细(建议收藏)
  15. aardio - 利用bitLock快速读写图片颜色值
  16. sql 语句 还原数据库
  17. 单向散列函数的实际应用
  18. SpringMVC框架 获取前台传过来的数组并解析
  19. 利达主机联网接线端子_利达消防设备接线图
  20. 字体css样式在线,CSS Fonts(字体)

热门文章

  1. Nature子刊:教你零基础开展微生物组数据分析和可视化
  2. 水泵泵宝显示下池缺水但是水池水很满_水泵控制器不能自动怎么办 显示下池缺水如何处...
  3. vue实战项目-喵喵电影 学习笔记(1)
  4. 【Excel】数据透视表—新增一列(字段)
  5. mysql导入数据报错_MySQL导入数据库时报错,MySQL server has go away
  6. Redmi K20 安卓9跨版本刷第三方ROM
  7. linux 使用cp卡死,细说CP使用
  8. Notice your ways of studing
  9. 联想T460p加装固态硬盘
  10. java计算器课程_Java课程设计——计算器团队博客