jerryinside介绍了一个
adobe的ajax框架,和当初见到rails一样,我几乎是看它一眼就爱上了。它是一个轻量级的ajax框架,以XML作为协议,目前功能还比较单一,不过几乎完全就是我所期待的。
Rails生成XML轻而易举,Rails 1.2的REST也是一项另人心动的功能,而Spry也是以资源作为操作对象,这2者结合会发生什么?我试着学习Spry的内涵,并结合Rails做点测试性应用,以确定是否值得迁移稍大点的项目到它上面。
首先建立一个项目:
rails test -d progresql
修改配置,让它正确连接到数据库,建立相应数据库。
进入项目文件夹,安装rails 1.2
rake rails:freeze:edge TAG=rel_1-2-0_RC1
既然要测试rest,就用scaffold_resource吧:
script/generate scaffold_resource post title:string body:text created_at:time
script/generate scaffold_resource comment post_id:integer body:text created_at:time
它自动在config/routes.rb里面添加了2行:
map.resources :posts
map.resources :comments
我的post和comment是一对多关联的,所以这里做点修改:
map.resources :posts do |post|
post.resources :comments
end
并且修改Post和Comment这2个model类:
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
因为上面已经指定了几个字段,所以它为我们生成了数据库模式,把它导入数据库:
rake db:migrate
修改app/views/posts/index.rhtml:
<h1>Listing posts</h1>
<table>
<tr>
<th>Title</th>
<th>Body</th>
<th>Created at</th>
<th>Comments</th> # <======
</tr>
<% for post in @posts %>
<tr>
<td><%=h post.title %></td>
<td><%=h post.body %></td>
<td><%=h post.created_at %></td>
<td><%= link_to 'Comments', comments_path(post) %></td> # <======
<td><%= link_to 'Show', post_path(post) %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post_path(post), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New post', new_post_path %>
打标记的2行是我添加的。
接下来在CommentController里添加点代码:
before_filter :find_post
...
#最后面
protected
def find_post
@post = Post.find(params[:post_id])
end
查找所有的comment_path,添加一个参数@post,比如comment_path(comment),就改成comment_path(@post, comment),包括所有的rhtml文件,我不知道有没有更方便的做法,不过我没找到命令行生成时指定这个参数。
查找所有的Comment.find(...),修改为@post.comments.find(...)。create方法修改成这样:
def create
@comment = @post.comments.create(params[:comment]) # <=====
respond_to do |format|
unless @comment.new_record? # <=====
flash[:notice] = 'Comment was successfully created.'
format.html { redirect_to comment_path(@post, @comment) }
format.xml { head :created, :location => comment_path(@post, @comment) }
else
format.html { render :action => "new" }
format.xml { render :xml => @comment.errors.to_xml }
end
end
end
标记的2行是我修改的。修改就完成了,下面测试一下。
启动服务器,打开 http://localhost:3000/posts,点击“New Post”添加几条记录。
接着点击每条记录后的Comments链接,各添加几条评论。
这才刚完成服务端代码。下面是视图处理,这里不打算演示太多功能(实际上我也没研究出多少
)。
把Spry的几个脚本文件拷到public/javascripts下。
打开app/views/layout/posts.rhtml,添加2行:
<%= javascript_include_tag 'xpath' %>
<%= javascript_include_tag 'SpryData' %>
这个演示只需要这2个文件。
把app/views/posts/index.rhtml备份一下,然后打开修改成这样:
<h1>Listing posts</h1>
<script language="javascript">
var dsPosts = new Spry.Data.XMLDataSet("posts.xml", "posts/post")
var dsComments = new Spry.Data.XMLDataSet("posts/{dsPosts::id}/comments.xml", "comments/comment")
</script>
<table spry:region="dsPosts">
<tr>
<th>Index</th>
<th onclick="dsPosts.sort('title', 'toggle')">Title</th>
<th>Body</th>
<th onclick="dsPosts.sort('created-at', 'toggle')">Created at</th>
</tr>
<tr spry:repeat="dsPosts" onclick="dsPosts.setCurrentRow('{ds_RowID}')">
<td>{ds_RowNumberPlus1}</td>
<td>{title}</td>
<td>{body}</td>
<td>{created-at}</td>
</tr>
</table>
<hr />
Comments:
<div spry:region="dsComments">
<div spry:repeat="dsComments">
{body}<br />
Comment At: {created-at}
<hr />
</div>
</div>
这短短一点代码完成了什么?
1、ajax抓取post数据
2、点击post记录行时,下面的Comments区域将动态请求服务器,提取属于该post的comments,Cool!它也会自动判断数据是否请求过,如果已经请求过则不会再请求。
3、点击表头中的"Title"和"Created At"这2个字段,表格会排序,Cool!
暂时只研究了这么些。