如何在特定页面中禁用Turbolinks? [英] How to disable Turbolinks in a specific page?
问题描述
我遇到一个问题,脚本只在刷新页面时起作用,所以我试图仅为该页面禁用Turbolinks。下面的代码不起作用。但是,如果我将data-no-turbolink属性直接添加到application.html.erb中的body标签,则可以正常工作。如何在我的视图中禁用Turbolinks?我已按照此处发布的解决方案 Rails 4:在特定情况下禁用Turbolinks页面但我无法让它工作。
I have an issue with a script only working when refreshing the page and so I'm trying to disable Turbolinks for only that page. The code below doesn't work. However, if I add the "data-no-turbolink" attribute directly to the body tag in application.html.erb it works. How do I disable Turbolinks in my view? I have followed the solution posted here, Rails 4: disable Turbolinks in a specific page but I can't get it to work.
我安装了宝石'jquery-turbolinks'。
I have the gem 'jquery-turbolinks' installed.
policy.html.erb
policy.html.erb
<% content_for :body do %>
<% if controller.controller_name == 'pages' && controller.action_name == 'policy' %>
<body data-no-turbolink="true">
<% end %>
<% end %>
<div class="row">
<div class="small-12 medium-8 large-7 columns end">
<a href="//www.com/" class="nostyle" title="Policy">Policy</a>
<script>(function (w,d) {var loader = function () {var s = d.createElement("script"), tag = d.getElementsByTagName("script")[0]; s.src = "//cdn.java.com/java.js"; tag.parentNode.insertBefore(s,tag);}; if(w.addEventListener){w.addEventListener("load", loader, false);}else if(w.attachEvent){w.attachEvent("onload", loader);}else{w.onload = loader;}})(window, document);
</script>
</div>
</div>
推荐答案
您可以在布局中使用此oneliner:
You can use this oneliner in your layout:
<body <%= "data-no-turbolinks='true'".html_safe if controller_name=="pages" && action_name=="policy" %>>
但另一种方法(我主要使用它)就是放入这一块代码到这个页面的链接...
But the other way to do it, which I use mostly, would be to put this chunk of code to the links leading to this page...
所以,假设你的路线名为:policy
,你应该这样做:
So, let's suppose your route is named :policy
, you should do this:
<%= link_to "Policy", policy_path, :"data-no-turbolink" => true %>
已经很久了,这是更新
最近,我开始使用 turbolinks 5.0 beta
,用:
Recently, I have started using turbolinks 5.0 beta
, by:
gem 'turbolinks', '~> 5.0.0.beta'
它变得容易得多......所有文件准备
javascript被加载,没问题...你所要做的就是为load事件添加一个监听器。
It gets far easier... All document ready
javascript gets loaded, no problem... All you have to do is add a listener to the load event.
$(document).on('turbolinks:load', named_function );
var named_function = function() {
// thinks to do on document load
}
您不必另外添加
$(document).ready(function (){
// stuff
});
或
$(document).ready(named_function);
因为 Turbolinks
会优雅地回归文档加载
如果页面是硬加载的。
Because Turbolinks
will gracefully fall back to document load
if the page is hard loaded.
这篇关于如何在特定页面中禁用Turbolinks?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!