启用 turbolinks 重新渲染特定的 js 文件 [英] Re-rendering specific js file with turbolinks enabled

查看:35
本文介绍了启用 turbolinks 重新渲染特定的 js 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rails 4 并且想使用内置的 Turbolink gem.我对它所做的一切都很满意,只是我希望它在每个页面请求时重新加载一个 js 文件.

I'm working with Rails 4 and would like to use the built in Turbolink gem. I'm happy with all it does except I'd like it to reload one of the js files upon every page request.

换句话说,我想要一个特定的 js 文件来忽略 turbolinks,我该怎么做?

推荐答案

您可以使用来自 turbolinks 的 'page:load' 事件将初始化函数连接到它.当 turbolinks 完全重新加载您的新页面时,它将始终被调用.

You can use 'page:load' event from turbolinks to connect an initialize function to it. It will be called always when turbolinks fully reaload your new page.

function initialize() {
  //js code what you want run always
}

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

如果你经常使用 jQuery,安装在你的 Gemfile 中很有用

If you use jQuery a lot, it is useful to install in your Gemfile

gem 'jquery-turbolinks'

和你的 application.js

and your application.js

//= require jquery
//= require jquery.turbolinks

Turbolinks 有另外两个有用的事件:

Turbolinks has two other useful events:

page:fetch //when new page begin to download
page:change //when the page has changed over

这篇关于启用 turbolinks 重新渲染特定的 js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆