通过JSON / AJAX更新HTMLJSON、AJAX、HTML

由网友(今日回味仍然吸引)分享简介:我一直在使用JSON来处理Ajax功能在我的Rails应用程序自从我听说这件事,因为使用RJS /渲染HTML感觉错误的,因为它违反了MVC。第一个AJAX重的项目,我的工作就结束了与直接连接到特定的UI-行为20-30控制器动作,我的看法codeS $ P $垫在控制器动作,泛音和RJS文件。使用JSON可以让你保持...

我一直在使用JSON来处理Ajax功能在我的Rails应用程序自从我听说这件事,因为使用RJS /渲染HTML感觉错误的,因为它违反了MVC。第一个AJAX重的项目,我的工作就结束了与直接连接到特定的UI-行为20-30控制器动作,我的看法codeS $ P $垫在控制器动作,泛音和RJS文件。使用JSON可以让你保持视图特定code在视图中,并只说通过AJAX查看不可知/ REST风格的控制器操作来获得所需的数据。

I've been using JSON to handle AJAX functionality in my rails applications ever since I heard about it, because using RJS/rendering HTML "felt" wrong because it violated MVC. The first AJAX-heavy project I worked on ended up with 20-30 controller actions tied directly to specific UI-behaviors and my view code spread over controller actions, partials and rjs files. Using JSON allows you to keep view specific code in the view, and only talk to view agnostic/RESTful controller actions via AJAX to get needed data.

该一个头痛的我发现使用纯JSON是,你必须使HTML通过JS,这在AJAX的,有更新DOM-重元素的情况下,可以是一个真正的痛苦。我结束了一长串的建筑code像

The one headache I've found from using pure JSON is that you have to 'render' HTML via JS, which in the case of AJAX that has to update DOM-heavy elements, can be a real pain. I end up with long string building code like

// ...ajax 
success: function(records){
  $(records).each(function(record){
    var html = ('<div id="blah">' + record.attr +
      etc +
    ')
  })
}

在这里等是10-15线的动态构建基于HTML的记录数据。除了烦恼,更严重的平局回到这个做法是HTML结构的重复(在模板和爵士)。*是否有这种方法的一个更好的做法?

where etc is 10-15 lines of dynamically constructing HTML based on record data. Besides of the annoyance, a more serious draw back to this approach is the duplication of the HTML structure (in the template and in the JS).* Is there a better practice for this approach?

(我的动机,最终到达了是我现在负责更新HTML这样复杂的IT要求的红宝石code两个嵌套循环首先呈现。复制那些在Javascript似乎疯了。)

(My motivation for finally reaching out is I am now tasked with updating HTML so complex it required two nested loops of Ruby code to render in the first place. Duplicating that in Javascript seems insane.)

有一件事情我已经考虑直接从文件系统加载的静态部分的文件,但是这似乎有点多。

推荐答案

只是偶然发现正是我一直在寻找:的 JAML

Just happened to find exactly what I was looking for: Jaml

阅读全文

相关推荐

最新文章