The company I work with recently upgraded from Rails 2 to Rails 3, during the same period we also migrated from rjs to jquery. We are adopting a new sample that aims to improve the reuse of the species. I will use edit / update as an example to illustrate the new way and the old way to create AJAX in a project:
and. The old way . Editing an action template does not provide the client with JavaScript for each specific update. The update template displays both the html content and the action (replace the element). this is very similar to the RJS approach, but in jQuery.
Pros:
1. AJAX html content and javascript code that manipulates the DOM are organized into the same .rb file, which is easy to maintain.
2. Looking at the specifications can check if the proper jom manipulation js code has been processed in the AJAX response.
Cons:
1. Since html content and js code are connected and assume a certain DOM structure on the landing page, it is difficult to reuse it on another page.
B. A new way - the update action template for an AJAX request returns only a JSON document that contains escaped html content. The js code that controls the DOM is displayed from the AJAX edit and listen action template: a success / error virtual event. Therefore, content and behavior are separated.
Pros:
- AJAX html javascript () , .
:
html js- 2 (js , html ), , .
- AJAX html, , , ().
JSON, , , html, JSON, JSON API.
, , , -, , - .
!
-Xiaotian