After reading David Heinemeier Hansson's blog post about server-generated javascript I decided to review the way I approach making AJAX calls in my Rails applications. What David suggests is to create a .js.erb template, which is just javascript embedded with ruby code generated on the server, and not to do any DOM manipulation in the client-side javascript.

在阅读了David Heinemeier Hansson关于服务器生成的javascript的博客后,我决定回顾一下在Rails应用程序中调用AJAX的方法。David的建议是创建一个。js。erb模板,它是嵌入在服务器上生成的ruby代码的javascript,在客户端javascript中不做任何DOM操作。

Another approach is of course to simply do everything on the client-side, and (for example) return a JSON object representing the updated object from the server and use javascript to do all DOM manipulation.

另一种方法当然是简单地在客户端上执行所有操作,然后(例如)返回一个JSON对象,该对象表示服务器上更新的对象,并使用javascript执行所有DOM操作。

I dislike the first approach for two reasons:

我不喜欢第一种方法有两个原因:

1) I use HAML and Coffeescript in my application, and feel that by using vanilla javascript and ERB would uncecessarily bloat my code-base with code of a different language (maybe it's possible to create .coffee.haml templates rather than js.erb, I don't know)

1)我在我的应用程序中使用HAML和Coffeescript,并且感觉使用普通的javascript和ERB会让我的代码库膨胀成另一种语言的代码(也许可以创建.coffee)。haml模板而不是js。我不知道erb)

2) I really don't like the idea of 'littering' my view folder with what is essentially javascript files, with a little bit of ruby embedded.

2)我真的不喜欢用javascript文件来“乱扔”我的视图文件夹,里面有一点点ruby。

The second approach, as David talks about in his blog post, relies very heavily on client-side javascript, which could lead to bloated client-side javascript code, and possibly the need for client-side templates, which in worst case scenarios could mean almost doubling the number of templates.

正如David在他的博客文章中提到的,第二种方法严重依赖客户端javascript,这可能导致客户端javascript代码膨胀,可能还需要客户端模板,在最坏的情况下,这可能意味着几乎要将模板数量增加一倍。

The approach I decided to go for (and want to ask whether or not is completely stupid way to go about this) is the following:

我决定采用的方法(并想问一下是否完全愚蠢的做法)如下:

1) Set the remote: true flag to make links and forms utilize AJAX to post to the server.

1)设置远程:真正的标志,使链接和表单利用AJAX发布到服务器。

2) In my controller, handle everything as html, and simply render without layout should the request be an AJAX request: render partial: '<partial-name>', layout: false if request.xhr?. This simply returns the HTML of the partial, with the ruby code evaluated.

2)在我的控制器中,把所有东西都当作html来处理,如果请求是AJAX请求,就简单地呈现:呈现部分:'< / partial-name>',布局:false if request.xhr?这只返回部分的HTML,并计算ruby代码。

3) In an asset javascript file (for instance <partial-name>.js.coffee) listen to ajax:success and append the HTML from the response.

3)在一个资产javascript文件中(例如 .js.coffee)听ajax:成功并在响应中附加HTML。

I like this approach because (in my rather simple application) this allows me to keep all my code in HAML/Coffeescript, and avoids any javascript templates.

我喜欢这种方法,因为(在我相当简单的应用程序中)这允许我将所有代码保存在HAML/Coffeescript中,并且避免使用任何javascript模板。

I realize this problem might take on a different character should the complexity of the application grow, but I still feel that it's a valid question: is this a bad way to go about implementing an AJAX-based architecture for a Rails application (and if so, why? i.e. is there a reason why returning HTML instead of JSON from an AJAX call is a bad idea?) or is this something I should continue to utilize?

我意识到,如果应用程序的复杂性增加,这个问题可能会有不同的特点,但我仍然认为这是一个有效的问题:这是为Rails应用程序实现基于ajax的体系结构的糟糕方式吗?例如,从AJAX调用中返回HTML而不是JSON是否有原因?

Thank you :-)

谢谢:-)

2 个解决方案

#1


2

Your approach seems to me quite accurate. However I would change 1 pt. Instead of using remote: true, I would use jQuery ajax call directly.

在我看来,你的方法相当正确。但是我将更改1 pt。我将直接使用jQuery ajax调用,而不是使用remote: true。

$(function(){
   $('#some_link').click(function() {
       $.ajax({
          // some parameters here
      })
      .done(function(data){
           $('div').html(data);
      });
   });
});

更多相关文章

  1. arcgis api for js入门开发系列十 自定义Navigation控件样式风格
  2. 如何访问远程节点。浏览器中的js应用程序,而不是本地主机
  3. 五十行javascript代码实现简单的双向数据绑定
  4. 在内容可编辑DIV中的选定文本周围包装bb代码
  5. 常用验证JS代码基础及实例
  6. 试着在我的javascript代码中理解“this”(一件事有效,另一件没有)
  7. 代码点火-如何从控制器返回Json响应
  8. 小弟初学网页(javascript),看不懂下面的代码。但又要完成任务 各位
  9. 使用jquery在textarea中的颜色代码

随机推荐

  1. android之Toolbar使用详解
  2. 日历
  3. 【Android】移动GIS开发必备(文档、帮助、
  4. 形状图形(shape)
  5. Android——实现两个控件水平居中
  6. Android:Service
  7. 修改ListView 分割线Seperator line
  8. Android 开启闪光灯做手电筒 源码
  9. android控件EditText
  10. Android--Listview横向滑动