使用turbolinks重新呈现特定的js文件 [英] Re-rendering specific js file with turbolinks enabled

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

问题描述

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



换句话说,我会就像一个特定的js文件忽略turbolinks一样,我该怎么做呢?

可以使用'page:加载'事件从turbolinks连接初始化函数。它会在turbolink完全实时加载新页面时被调用。

 函数initialize(){
// js代码你想要运行的总是


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

如果您使用jQuery的次数很多,那么安装在您的Gemfile中是很有用的。
$ b

  gem'jquery-turbolinks'

和您的application.js

  // =需要jquery 
// =需要jquery.turbolinks

Turbolinks有两个其他有用的事件:

  page:fetch //当新页面开始下载时
页面:当页面改变超过
时发生变化//


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?

解决方案

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

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

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