2008-07-23

rjs 技术

关键字: rjs 技术
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 ;
   },

  • Ajax_on_Rails_普通下载_.rar (1.5 MB)
  • 描述: 今天才想起来把这本书给大家看!上面讲的不错,代码直接有些可以使用。
  • 下载次数: 2
评论
dogstar 2008-07-24
 var a = new Ajax.Request(
"/people/message", // 调用 people controller 下的 message 方法 
这个不就是prototype么

楼下的楼下这个问题也问的欠水准,rjs本来就是用的prototype,何必问?
xiaotian_ls 2008-07-23
也是用的prototype吧
发表评论

您还没有登录,请登录后发表评论

sunfengcheng
搜索本博客
博客分类
存档
最新评论