问题标签 [turbolinks]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
144 浏览

javascript - JS 脚本仅在我不更改当前页面时有效

我正在 Ruby on Rails 中创建一个 Web 项目。我有一个main.js文件存储在里面/app/assets/javascripts/,我在其中开发了一个小脚本来重置表单。

当我不在网上冲浪时,该脚本可以完美运行。如果我更改当前页面,脚本将不再工作。为了让它再次工作,我必须通过浏览器重新加载页面(使用Cmd+RF5)。

main.js脚本应该被编译并且应该在项目的所有页面中都可用,不是吗?

怎么了?

中的代码main.js

应用程序.js:

0 投票
2 回答
1395 浏览

ruby-on-rails - Rails : Backbone + Turbolinks Hybrid Routing

I'm building a Rails 4 application and I'm trying to combine location.hash Backbone History with turbolinks push state. The application is broken up into multiple smaller, SPA like pages.

Example of Problem

The problem I'm having is that when I do Backbone.Router.navigate(), within a page, it doesn't register anything with turbolinks. Here's a hypothetical example to demonstrate the problem:

  1. Visit /one
  2. @router.navigate 'page-one'
  3. Location becomes /one#page-one
  4. @router.navigate 'page-two'
  5. Location becomes /one#page-two
  6. Click on link to /two
  7. Turbolinks intercepts and navigates to /two
  8. Click on the Back Button, you are still stuck on /two
  9. Click on the Back Button again, you are still stuck on /two
  10. Click on the Back Button a third time, you are brought back to /one

What I Tried

I guessed that what's probably happening is that when navigating via backbone, the browser is logging the change but turbolinks isn't. I tried to expose Turbolinks.reflectNewUrl:

turbolinks.js.coffee

And then modifying Backbone.Router.navigate so that it registers with turbolinks every time we navigate:

That sort of worked, Step 8 above is no longer stuck on /two, but click on the back button again and we get this error:

So something is still wrong.

Now I'm hoping there's a kind soul out there to help me out and point out my perhaps obvious blunder :)

0 投票
2 回答
882 浏览

javascript - Ruby-on-Rails JavaScript 未在 link_to 上加载

我正在使用 Google 可视化在嵌套在视图中的脚本标记中构建图表表。这在直接获取页面时效果很好,但是当通过link_to帮助程序运行时,javascript 不起作用。

我相信这与 turbolinks 有关,但是用 a 包装如下 JavaScript page:load并没有什么不同。我已经确认关闭 turbolinks 确实可以解决这个问题,但是如果没有它,整个应用程序会明显变慢。

0 投票
2 回答
1563 浏览

ruby-on-rails - Bootstrap 3 模态背景使用 Turbolinks 变得更暗

我在 rails4 上使用带有 Bootstrap 3 Modal 组件的 Turbolinks。如果我刷新并打开页面,模态背景将是正常的。但是,当我打开其他页面时,模态背景不透明度会变得越来越暗,我假设当 Turbolinks 在另一个页面中加载时不透明度没有重置。我试图找到背景元素,但看起来不透明度是由 javascript 动态生成的。有没有人看到同样的问题?解决方法是什么?

0 投票
2 回答
1594 浏览

ruby-on-rails - Rails link_to with turbolinks and the new hash style

this works nicely

and translates to

I want to change it to the new hash syntax so I did this:

but it translates to

EDIT: This Works:

which translates to

but shouldn't I stick to link_to rather than <a href></a>?

0 投票
1 回答
663 浏览

javascript - Ruby on Rails:仅在启用了 turbolinks 的已定义页面中呈现 javascript

我将 javascript 嵌入到使用 Turbolinks 的 Rails 视图中,例如:

ViewX.html.haml:

加载 ViewX 时一切正常。但是当我导航到查看 YI 时,控制台中出现以下错误:

当我导航到其他视图时,该错误仍然存​​在。它在执行硬刷新时消失,但在我下次渲染 ViewX 时返回。

我的问题是如何让一些 JS 片段仅针对特定视图呈现而不污染应用程序的其余部分?

编辑

好的,我想出了一个方法,这有点像 hack,但包含 hack。将 ViewX.html.haml 更改为:

ViewX.html.haml:

0 投票
8 回答
9066 浏览

ruby-on-rails - Rails 4 turbolinks 与谷歌分析

我想知道在 Rails 4 中实现 Google Analytics 跟踪代码以及 turbo 链接的最佳方法是什么。正常的代码段可以工作吗?我也看到了这方面的宝石,但我不确定它的作用。

0 投票
1 回答
169 浏览

jquery - 在rails应用程序中导航时jQuery未执行

我在第一次导航到 root_path 和/或重新加载页面时成功运行的 rails 应用程序中有 jQuery/Coffeescript,但是,如果我使用主页上的链接导航到另一个页面,然后导航回主页, jQuery(应该在页面完成加载时运行)不运行。为什么是这样?我怎样才能让它在每次点击页面时运行?谢谢!

'/home.js.coffee'

注意:我使用的是 Ruby 2 和 Rails 4

应用程序.js

0 投票
1 回答
3696 浏览

javascript - Turbolinks:Rails 4 显示慢速页面进度的方式是什么?

这个问题听起来可能类似于Rails turbolinks long request doesn't show page load

我最近启动了新的 Rails 4 应用程序,它默认使用 Turbolinks 库 - 它很棒:让应用程序感觉更快/更快捷,但是我确实有一些页面比其他页面慢。

不幸的是,我找不到让浏览器显示默认加载指示器的好方法——就像它在加载“常规”网页时所做的那样。人们推荐不同的 JavaScript 解决方案,例如https://github.com/caarlos0/turbolinks_transitions。虽然有些看起来不错,但“普通互联网用户”不习惯在默认浏览器的加载指示器以外的地方看到加载进度。

  • 如何让 Chrome 在选项卡标题中显示“旋转半圆”?
  • 或者我怎样才能让 Safari 在地址栏中显示“移动的蓝色波浪”?
  • 等等。

不过,我的大多数页面都很快,所以我想为那些不会在 300 毫秒内加载的页面显示这个加载指示器,例如。

谢谢!亚历克斯。

0 投票
5 回答
5485 浏览

ruby-on-rails - 使 jQuery 与 Turbolinks 一起工作

我有一个 Rails 4 应用程序,它使用Turbolinks. 我的理解是,这Turbolinks会破坏 jQuery 代码,因为 Turbolinks 不会加载新页面,而只会获取新元素。

因此,导航到新页面可能不会触发.ready,尽管它总是会触发.page:load,因此新的 jQuery 代码不会初始化。

我有很多 jQuery 代码,所以我不想修改我的 jQuery 代码以与 Turbolinks 兼容。

是否可以向我的 application.js 添加一个 javascript 代码来覆盖.ready要包含page:load的事件?我该怎么做?