简体   繁体   English

如何在Rails中立即定期执行Ajax查询

[英]How to execute ajax query immediately and periodically in Rails

In Rails 4 I have index and show views for a class (zones) that have fairly complex javascript to update values. 在Rails 4中,我具有索引并显示具有相当复杂的javascript来更新值的类(区域)的视图。 For DRYness, the HTML just creates a data-less layout and the js file inserts the data, modifies the style, and so on. 对于DRYness,HTML只会创建一个无数据的布局,而js文件会插入数据,修改样式等。 This data needs to be updated every 15 seconds or so. 此数据需要每15秒左右更新一次。 A common.js.coffee file contains common.js.coffee文件包含

root = exports ? this
root.executeQuery = (isAsync = true) ->
  if $('#ajaxParams')? and $('#ajaxParams').data('model')?
  # Use dynamic updates
  update_url = '/' + $('#ajaxParams').data('model')
  if $('#ajaxParams').data('id')?
    update_url += '/' + $('#ajaxParams').data('id')
  update_url += '.js'
  $.ajax({url: update_url, async: isAsync, dataType: "script"}).done (content) ->
    setTimeout ( ->
      executeQuery()
    ), 15000
else
  setTimeout ( ->
    executeQuery()
  ), 15000

which handles the periodic update. 处理定期更新。 Then zones.js.coffee contains 然后zones.js.coffee包含

$(document).ready ->
  executeQuery(false)

So on page ready, the query is run, the dynamic content is inserted, the timer is scheduled and then updates periodically. 因此,在准备好页面时,将运行查询,插入动态内容,安排计时器,然后定期更新。 So far, so good, But when I click on aa link that takes me to the zones show method, the page is data-less until the timer kicks off the ajax request. 到目前为止,一切都很好,但是当我单击将我带到zone show方法的链接时,页面将没有数据,直到计时器启动了ajax请求。 There is no page ready event. 没有页面准备事件。 The same thing happens when I click on a show link to go back to the index. 当我单击显示链接返回索引时,也会发生相同的情况。

How do I get these views to always update immediately, or at least cache their previous data, and then do their periodic update? 如何获取这些视图以始终立即更新,或至少缓存其先前的数据,然后进行定期更新?

There is no page ready event. The same thing happens when I click on a show link to go back to the index

This problem will be caused by Turbolinks 此问题将由Turbolinks引起

Turbolinks basically keeps the <head> tags on your page, and loads the <body> via ajax. Turbolinks基本上将<head>标记保留在页面上,并通过ajax加载<body> Although it offers some performance benefits, it causes all sorts of issues trying to load JS dynamically. 尽管它提供了一些性能上的好处,但它会导致尝试动态加载JS的各种问题。 To fix this, you'll need to: Rails 4: how to use $(document).ready() with turbo-links : 要解决此问题,您需要: Rails 4:如何在Turbo-links中使用$(document).ready()

$(document).ready ->
  executeQuery(false)

Should be: 应该:

$(document).ready(executeQuery)
$(document).on('page:load', executeQuery)

You'll need to encapsulate your function in a variable, like this: 您需要将函数封装在一个变量中,如下所示:

executeQuery = -> 

    (isAsync = true) ->
       if $('#ajaxParams')? and $('#ajaxParams').data('model')?
           # Use dynamic updates
           update_url = '/' + $('#ajaxParams').data('model')
       if $('#ajaxParams').data('id')?
          update_url += '/' + $('#ajaxParams').data('id')
          update_url += '.js'
          $.ajax({url: update_url, async: isAsync, dataType: "script"}).done (content) ->
             setTimeout ( ->
                 executeQuery()
             ), 15000
      else
             setTimeout ( ->
                 executeQuery()
             ), 15000

I think you'll have to rename your function name (I could not glean the exact context from your question only), as you seem to be self-referencing 我认为您必须重命名函数名称(我无法仅从您的问题中收集确切的上下文),因为您似乎是自引用的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM