DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> Rails的RJS模板七
Rails的RJS模板七
編輯:AJAX詳解     

三、Rendering – 渲染

有許多選項用於在一個控制器內的render 調用,但是你不必因RJS而關心它們中的大多數。表格一總結了ActionController 內的 render()調用,並且顯示了某個選項何時可使用在RJS上下文環境內。

There are a lot of options to the render call within a controller, but you don't have to worry about most of them with RJS. Table 1 summarizes the options to the render() call in ActionController, and shows whether or not each particular option is useful in the context of RJS.

Table 3-1. Options available when rendering RJS templates

Option

Works with RJS?

Returns Content-Type = text/Javascript?

:action

Works as expected

Yes

:template

Works as expected

Yes

:file

Works as expected

Yes

:inline

Not useful for RJS

No

:partial

Not useful for RJS

Yes

:text

Not useful for RJS

No

四、Inline Rendering – 內聯渲染

Rails 也支持渲染給控制器的內聯RJS模板。這個服務必須創建一個完全的.rJS 文件來真正地把任務簡化為一行或兩行代碼。

Rails also has support for inline RJS template rendering to the controller. This saves having to create an entire .rJS template file for those really simple one- or two-line tasks.

內聯RJS渲染通過從控制器內傳遞 :update 參數給 render() 來完成。通常是把定義在一個RJS模板內的東西聲明在一個與 render() 調用關聯的代碼塊內。該代碼塊被傳遞一個 JavaScriptGenerator 的實例,就像在常規的 RJS模板內一樣。下面控制器動作用渲染局部模板 header 來更新替換 id 為header 的DOM元素的 innerHtml 。

Inline RJS rendering is performed by passing the :update parameter to render() from within the controller. What is normally defined in an RJS template is declared in a code block associated with the render() call. The code block is passed an instance of the JavaScriptGenerator, just like in normal RJS templates. The following controller action update replaces the innerHtml of the DOM element with id header with the rendered partial header.

def update

render :update do |page|

page.replace_Html, 'header', :partial => 'header'

end

end

完成渲染內聯意味著你不必通過搜索你的工程來查找一個 .rJS 模板以查看動作要完成什麼。內聯渲染在控制器內生成視圖代碼。如果內聯RJS代碼多於一或兩行,那麼明確地為該功能創建個RJS模板是明智的選擇。

Performing the render inline means that you don't have to go searching through your project for a .rjs template to see what the action does. Inline rendering does bring vIEw code into the controller. If the inline RJS code is more than one or two short lines, it is advisable to create an explicit RJS template for that functionality.

五、Browser Redirection – 浏覽器重定向

如果你需要在一個AJax請求期間重定向浏覽器,你必須查閱標准Rails之外的redirect_to() 方法。Prototype庫沒有像浏覽器做的那樣的應答HTTP狀態碼,並且不會跟隨3xx重定向狀態嗎。幸運的是, JavaScriptGenerator也提供了一個redirect_to()方法,它生成必要的 JavaScript 來重定向浏覽器。

If you need to redirect the browser during an AJax request, you have to look outside the standard Rails redirect_to() method. The Prototype library doesn't respond to HTTP status codes like a browser does, and doesn't follow the 3xx redirect status codes. Fortunately, the JavascriptGenerator also offers a redirect_to() method, which generates the JavaScript necessary to redirect the browser.

這個新的 redirect_to()方法在你的RJS模板內的page對象上調用,或者你控制器內的一個 update 塊上調用。通常你會更多地在你控制器內的內聯RJS上調用 redirect_to() 而很少在你的模板內調用,因為 redirect_to() 方法接受更多匹配給控制器。這個新方法的用法類似於你曾經使用過的標准的redirect_to() 方法,除了它的調用來自於 page 對象。讓我們看看幾個例子來顯示實踐中的RJS重定向是如何工作的。

This new redirect_to() method is called on the page object in your RJS template, or an update block in your controller. It is probably more common that you will be calling redirect_to() using inline RJS in your controller than in your templates, since the redirect_to() method is a task more suited to the controller. This new method is used just like the standard redirect_to() method that you are used to, except it is called from of the page object. Let's take a look at a few examples to show how redirecting with RJS works in practice.

render :update do |page|

page.redirect_to :controller => 'employees', :action => 'list'

end

這兒我們重定向浏覽器到 EmployeesController 的 list 動作。沒有什麼新東西或讓人驚奇的東西,類似於你通常使用的重定向,但該重定向方法是在page 對象上調用的,所以 JavaScriptGenerator 可以生成適當的JavaScript。當然,你必須在控制器的動作內放些代碼,或者是沒有渲染:update 塊的 RJS 模板。

Here we are redirecting the browser to the list action of the EmployeesController. Nothing new or surprising, just redirect like you normally would, but make the redirect method call on the page object, so that the JavascriptGenerator can generate the appropriate JavaScript. Of course, you have to put the code within a controller action, or without the render :update block in an RJS template.

You can also redirect to an absolute URL by passing in a string containing the URL.

render :update do |page|

page.redirect_to 'http://www.shopify.com'

end

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved