启用turbolinks重新渲染特定的js文件

时间:2022-09-05 20:12:02

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.

我正在使用Rails 4,并希望使用内置的Turbolink gem。我很满意它所做的一切,除了我希望它在每个页面请求时重新加载一个js文件。

In other words I'd like a specific js file to ignore turbolinks, how can I do this?

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

1 个解决方案

#1


7  

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.

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

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

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

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

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

gem 'jquery-turbolinks'

and your application.js

和你的application.js

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

Turbolinks has two other useful events:

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

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

#1


7  

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.

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

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

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

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

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

gem 'jquery-turbolinks'

and your application.js

和你的application.js

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

Turbolinks has two other useful events:

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

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