对不起,如果这有点复杂。我会尽力描述它,但如果我不清楚,请提问。我可以在我的Rails 3 js.erb文件中使用JS条件来渲染部分?
我正在开发一个需要自动更新状态页的Rails应用程序。此状态页面将包含请求,其中有许多尝试,其中有很多步骤。为了保持这个页面尽可能最新,我们使用JQuery来每5秒轮询一次服务器。
然后通过index操作将我们带到index.js.erb文件。这个文件目前被设置为确定(基于屏幕上的内容)哪些部分需要重新渲染。我使用屏幕上当前的内容来确定请求/尝试/步骤是否仍处于活动状态,以了解需要刷新的内容。我选择了这种方式,而不是使用数据库中的内容,因为数据库会将项目更新为非活动状态,导致屏幕上的活动项目不会更新以显示其处于非活动状态....但也许有更好的方法这个。
现在我的主要问题。在我的ERB文件,我有很多:
if (some statement) {<%= escape_javascript(render :partial=>"my_partial") %>}
其中,评估在脚本中提到的,无论什么情况下都谐音。这将会使我们的渲染时间呈指数级增长,并且似乎在我们的服务器上受到重创。
有没有一种方法可以根据屏幕上当前的内容有条件地渲染部分?
有没有更好的方法来解决这个问题呢?我知道动态更新的状态页面遍布各处,但无法找到一个很好的例子来引导我走向正确的方向。
此外,这是我的一些实际代码片段,也许提供了一个我正在做的更好的例子。我对复杂的JavaScript相当陌生,所以请让我知道,如果我有什么不完整的东西。
<%attempt.steps.each do |step| %>
/* If a status for this step already exists on the page, only update portions of it, otherwise append the step status to the page */
if (document.getElementById("step-status-<%= step.id %>")) {
$("#step-status-<%= step.id %>").html('<%= escape_javascript(render :partial=>'step_status', :locals=>{:step=>step}) %>');
<% if step.log.present? #log will be present once step is complete %>
/* If the log isn't displayed on the page, add the partial to display it. This is supposed to allow for this partial to only be rendered once and not refreshed*/
if ($("#step-<%= step.id %>-log pre").is(":empty")) {
$("#step-<%= step.id %>-log").html('<%= escape_javascript(render :partial=>'log_details', :locals=>{:step=>step}) %>');
$("#step-<%= step.id %>-log-button").html('<%= escape_javascript(link_to("View Log", "#", :id=>"step-#{step.id}")) %>');
}
<% end %>
} else {
$("#step-details-list-<%= attempt.id %>").append('<%= escape_javascript(render :partial=>"step", :locals=>{:step=>step}) %>');
}
<% end %>
非常感谢您的帮助。
崔西