Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

javascript - Re-rendering specific js file with turbolinks enabled

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

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

gem 'jquery-turbolinks'

and your application.js

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

Turbolinks has two other useful events:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...