【发布时间】:2015-08-12 20:19:35
【问题描述】:
我有这样的看法:
<!-- Loads custom Stylesheet -->
<%= stylesheet_link_tag 'simulation' %>
<!-- Loads polling calls to update -->
<script type="text/javascript">
<%= render 'show.js.erb' %>
</script>
<!-- Render simulation -->
<%= render 'simulation' %>
<%= link_to 'Back', simulations_path %>
它包含这两个部分:
_show.js.erb:
callUpdate = function(id) {
if (id) {
console.log("tic")
$.ajax({
type: "PUT",
url: "/simulations/" + id
});
} else {
console.log("Update error, no id passed.")
}
$('#sim_div').html("<%= j (render @simulation) %>");
setTimeout( function() {
callUpdate(id)
}, 5000);
};
setTimeout( function() {
callUpdate("<%= @simulation.id %>")
}, 5000);
_simulation.html.erb:
<div id="sim_div">
<h1><%= @simulation.identifier %></h1>
<h4 class="offset-col-sm-1">Dimensions: <%= @simulation.x_size %>x<%= @simulation.y_size %></h4>
<h4 class="offset-col-sm-1">Verdict: <%= @simulation.verdict %></h4>
<!-- REMOVE -->
<h1> <%= @simulation.dirty? %></h1>
<table class="table table-bordered">
<thead>
<tr>
</tr>
</thead>
<tbody>
<% @simulation.state.each_with_index do |row, y_index| %>
<tr>
<% row.each_with_index do |current, x_index| %>
<td class="text-center <%= 'dirty' if @simulation.dirty_points.include?([x_index,y_index]) %>"><%= current %></td>
<% end%>
</tr>
<% end %>
</tbody>
</table>
<br>
</div>
javascript 正在调用我的控制器更新函数,它在更新函数上设置脏记录。
simulation_controller#show:
def update
@simulation = Simulation.find(params[:id])
@simulation.next # <= THIS SETS DIRTY RECORDS ON THE OBJECT
@simulation.save
respond_to do |format|
format.js
format.html { redirect_to simulations_url }
end
end
我的 javascript 代码在更新调用之后永远不会获得新的更新记录,我希望它在他们不再更新调用时停止调用更新并停止轮询。但是它只能看到不脏的初始记录。只有 _simulation 部分似乎收到了新记录(即使它被 javascript 代码重新呈现,所以我不确定为什么 javascript 看不到更新的记录。)
如何访问更新记录的属性,而不仅仅是 javascript 代码中的原始记录?谢谢!
【问题讨论】:
标签: javascript ruby-on-rails ruby-on-rails-4 views partials