简体   繁体   English

更改视图后,Rails /咖啡脚本/数据表需要刷新以重新加载

[英]Rails/coffeescript/datatables, after changing views, need to refresh to reload

I'm sorry for asking this here as I typically find everything I need to ask, but I think my problem is I do not know really WHAT to search for to get the answer. 很抱歉在这里提出这个问题,因为我通常会找到我需要问的所有问题,但是我认为我的问题是我不知道要搜索什么才能真正找到答案。 Here goes. 开始。 I am playing with datatables to provide sorted/ajaxified/searchable tables. 我正在玩数据表以提供排序/ ajaxified /可搜索表。 Everything loads correctly when I hit the site. 当我访问该网站时,一切正常加载。 It is fine after I add data to the table or sort or search. 将数据添加到表或排序或搜索之后就可以了。

But, if I navigate from the page, say back to the home page, and then back to the page with the table, the table is still there, but the datatables stuff is gone. 但是,如果我从页面导航,说回到主页,然后再回到带有表的页面,则表仍然存在,但是datatables的内容消失了。 I assume this is a matter of me JUST starting to work with JS/Ajax and the like for my Rails app and I probably am just in need of something very simple. 我认为这只是我的问题,只是开始为我的Rails应用程序使用JS / Ajax之类的东西,而我可能只需要一些非常简单的东西。

The specific code I'm using is: 我正在使用的特定代码是:

$ ->
$("#customers").dataTable
sDom: "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>"
sPaginationType: "bootstrap"

If I watch the page load, I think I see the issue. 如果我看到页面加载,我想我看到了问题。 When it loads initially, or any time I send it back after adding a new customer, I see all the JS and CSS files load. 当它最初加载时,或者在添加新客户后的任何时候将其发回时,我都看到所有JS和CSS文件都已加载。 But, when I hit back, it's a cached call and it's not loading any of those again, so, I'm assuming I'm not "initializing" the JS again. 但是,当我回击时,这是一个缓存的调用,并且不会再次加载其中的任何一个,因此,我假设我不会再次“初始化” JS。

Any help would be appreciated. 任何帮助,将不胜感激。

So, my problem was really not knowing enough to know exactly what to ask. 因此,我的问题实际上是不够了解要问什么。 The problem here wasn't dataTables or JS or Ajax or any of that. 这里的问题不是dataTables或JS或Ajax或其他任何问题。 It was Turbolinks in Rails 4. Because it caches to make things seem fast, whenever I'd leave the page and come back to it, I'd have to refresh to get the underlying javascript to initialize. 它是Rails 4中的Turbolinks。因为它缓存使事情看起来更快,所以每当我离开页面并返回页面时,我都必须刷新以获取底层的javascript进行初始化。

Turbolinks has an option you can pass into your script to make a document ready on page:restore. Turbolinks提供了一个选项,您可以将其传递到脚本中以使文档准备好在page:restore。 That worked. 那行得通。 If anyone else runs into this maybe they'll find this helpful. 如果还有其他人遇到这个问题,他们会发现这很有帮助。

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

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