rjs 技术

[size=large][color=green]rjs 技术
rjs技术是伴随着ruby 做Ajax而被应运而生的,刚开是使用ruby在rails下进行 Ajax开发有点不适应,因为ruby和js文件的调用方法不是很清楚,我看了昨天说《Ajax on rails》这本书豁然开朗了不少, 伴随着对 JavaScript 深入了解,便很快掌握了,这里给几个例子。
参考文献:
http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro2
在 js 文件中定义一个函数 showmessage_onlyone 去调用 ruby下面的 message 方法,

javascript 代码:
showmessage_onlyone : function(schedule) {
var a = new Ajax.Request(
"/people/message", // 调用 people controller 下的 message 方法
{
"method": "get", // get 或 post 请求
"parameters" : "value="+"sadfasdf", // 传递的参数 ,在 ruby 方法中 直接 用 params 接受
//"parameters" : Form.serialize(popdiv), // 可以将整个 div 容器传过去
asynchronous: false, // 是不是异步执行 , 默认 true 异步执行, false 顺序执行
} );

}Ajax 对象 还用很多属性和方法,详情可 点击上面的参考文献,
ruby 文件中的 PeopleController? 中定义的 message方法代码:
def message 
@people= People.find(params[:id])
render :update do |page|
page.replace_html 'show_message1',:partial =>"list1"
end
end
执行 js 中的 showmessage_onlyone 函数 将调用 ruby 的 message 方法,

ruby代码
在 PeopleController? 下定义 find_last 的方法 去调用 js 文件中的 get_last 函数,
ruby方法定义如下,
def find_last 
last = People.find(:all).last
last_id = last.id
render :update do |page|
page << "get_last('#{last_id}')" #调用了 js 下面的 get_last 函数,并将last_id值传过去,
end
end

js 代码
在 application.js 文件中定义 get_last 函数,相应的view页面添加
<%= javascript_include_tag "application"%>get_last : function (last_id)
{
alert(last_id);
return last_id ;
},

[/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值