将HTML返回给AJAX Rails调用 [英] Returning HTML to AJAX Rails calls

查看:80
本文介绍了将HTML返回给AJAX Rails调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



1)I在我的应用程序中使用HAML和Coffeescript,并且觉得通过使用vanilla javascript和ERB会不断地使用不同语言的代码膨胀我的代码库(也许可以创建.coffee.haml模板而不是js.erb,我不会知道)



2)我真的不喜欢乱丢我的视图文件夹的想法,其中包含一些基本上是javascript的文件,其中嵌入了一点ruby。



第二种方法,正如大卫所说的那样回合他的博客文章,非常依赖客户端javascript,这可能导致客户端javascript代码膨胀,可能需要客户端模板,在最坏的情况下,这可能意味着模板数量几乎翻倍。



我决定采用的方法(并且想要询问是否完全是愚蠢的方式)如下:



1)设置 remote:true 标志,使链接和表单利用AJAX发布到服务器。



2)在我的控制器中,处理所有内容为html,如果请求是AJAX请求,则只需渲染而不显示布局:渲染部分:'< partial-name>',布局:如果request.xhr,则为false?。这只是返回部分的HTML,并评估了ruby代码。



3)在资产javascript文件中(例如< partial- name> .js.coffee )收听 ajax:success 并附上回复中的HTML。



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



我意识到如果应用程序的复杂性增加,这个问题可能会出现一个不同的问题,但我仍然认为这是一个有效的问题:这是一个不好的方法关于为Rails应用程序实现基于AJAX的架构(如果是这样,为什么?也就是说,为什么从AJAX调用返回HTML而不是JSON是个坏主意?)或者这是我应该继续使用的东西吗? / p>

谢谢: - )

解决方案

你的方法在我看来相当准确。但是我会换1点。而不是使用remote:true,我会直接使用jQuery ajax调用。

  $(function(){
$ ('#some_link')。click(function(){
$ .ajax({
//这里的一些参数
})
.done(function(data){
$('div')。html(data);
});
});
});


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.

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.

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)

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.

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.

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.

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.

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

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.

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?

Thank you :-)

解决方案

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.

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

这篇关于将HTML返回给AJAX Rails调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆