Rails的RJS模板九 (转)

Rails的RJS模板九

四、Collection Proxies – 集合代理

select() 方法返回一个DOM对象数组。Select() 接受一个基于CSS选择符的参数并且返回一个匹配这个选择符的DOM对象数组。因此要得到一个 id content <div> 内的所有段落,我们可以使用:

The select() method returns an array of DOM objects. select() takes as an argument a CSS-based selector and returns an array of DOM objects matching that selector. So to get all paragraphs within a <div> with id content, we would use:

page.select('#content p')

现在把这与可枚举方法结合起来可有效地遍历集合代理并且你可以做类似的事情:

Now combine this with the enumerable methods available through the collection proxy and you can do things such as:

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

element.hide

end

id content DOM内的所有 <p> 元素将被隐藏。

All <p> elements within the DOM element with id content will be hidden.

假设最近被添加的基于属性的选择符,它允许你基于它们的CSS属性来选择元素。

Support for attribute-based selectors was also recently added, which allows you to select elements based on their CSS attributes.

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

element.hide

end

这个代码隐藏做为祖先表单内所有的类型为textinput元素。

This code hides all of the input elements of type text that have an element with id form as an ancestor.

属性选择符支持 =, ~=, |=, existence, and != 。你可以同时使用多个选择符,比如 input[class=link][href=”#”]。但这些并不能工作在IE浏览器上。

The attribute selectors support =, ~=, |=, existence, and !=. You can use multiple selectors at the same time, such as input[class=link][href="#"]. The only downside to the selectors support is that they will not work in Internet Explorer.

五、Making Ajax Calls with Rails

如果你不从你的页面出Ajax调用的话,RJS不是非常有用的。Rails提供了许多方式来完成Ajax调用并且适合于每种特定的情况。下面方法的更多信息可参阅RoR文档。

RJS wouldn't be very useful if you couldn't make Ajax calls from your pages. Rails offers many ways to perform Ajax calls and each is particularly suited to a particular situation. See the Ruby on Rails documentation for more information on the following methods.

1link_to_remote(name, options = {}, html_options = {})

这是RailsAjax调用的常见方式。Link_to_remote()是辅助方法,它生成一个超链接,在点击它时生成一个Ajax调用。<a>标记的onclick()事件内Rails即可生成一个Ajax.Request也可生成一个Aajx.Update,这依赖于是否有 :udpate 选项传递给 link_to_remote()方法。对于RJS目的,:update 选项不应试被使用因为 Prototype Ajax.Update 对象期望一个HTML应答,而RJS返回一个 JavaScript 应答。如果你页面的RJS应答出现了各种怪异问题的话,你或许在RJS模板内使用了 :update 选项。

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.

2link_to_function(name, function, html_options = {})

生成一个超链接,当点击时,它运行一个 JavaScript 函数或代码。这实际上并不创建一个Ajax应答,但它可以被用于运行定制的 JavaScript 函数。使用这个方法来调用你定制的 JavaScript 库,该库使用 Ajax.Request 来产生 Ajax 调用。

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.

3observe_field(field_id, options = {})

观察一个字段并在内容被修改时或用 :on 指定的事件发生时,做出一个Ajax请求。

Observes a field and makes an Ajax request when the content has changed or the event specified with :on has occurred.

posted @ 2009-06-17 19:59  麦飞  阅读(516)  评论(0编辑  收藏  举报