我正在测试Turbolinks是否可以帮助我们的 Web 应用程序看起来更快一点,表现得“几乎像”一个单页应用程序。
由于我们已经在所有链接上的所有点击上有了一种“主钩”,我设置data-turbolinks="false"了<body>然后更改了我们的点击处理程序以调用 Turbolinks:
function goToUrl(url) {
// some controls here, and then ...
Turbolinks.visit(url);
}
Run Code Online (Sandbox Code Playgroud)
我还禁用了缓存并将根设置为/:
<meta name=\"turbolinks-cache-control\" content=\"no-cache\">
<meta name=\"turbolinks-root\" content=\"/\">
Run Code Online (Sandbox Code Playgroud)
但是,我注意到它总是执行标准的页面更改,而不是它的“魔法”。
如果我从控制台调用该方法,也会发生同样的情况,没有显示错误或警告,但页面加载正常,而不是使用 Turbolinks“魔术”。
它唯一有效的时间是访问根地址 ( "/") 时。
Is there some more configuration involved, than simply using the visit() method?
EDIT:
After further testing, I noticed that, when the call to Turbolinks.visit(url) seems to "fail", by issueing a normal page navigation instead, it returns an object like that …
javascript hyperlink single-page-application turbolinks turbolinks-5
我已经使用 Ruby 2.6.1 从我的 Rails 6.0.0 beta Web 应用程序中删除了 turbolinks,这样做后,每当我更新或创建章节时,POST 请求都会发送到数据库。但是虽然数据库会保存数据,它也会尝试重定向页面,但浏览器不会重定向到任何地方,仍然会显示编辑/新页面。
在移除 turbolinks 之前,这不是问题。我试过四处寻找,但到目前为止找不到任何解决方案。我尝试重新启动服务器,当然bundle update事先使用以实际删除turbolinks。
章节控制器
def update
@book = Book.find(params[:book_id])
@chapter = @book.chapters.find(params[:id])
if @chapter.update(chapter_params)
redirect_to book_chapter_path(@book, @chapter)
else
...
end
end
...
private
def chapter_params
params.require(:chapter).permit(:title, :content, :status)
end
Run Code Online (Sandbox Code Playgroud)
章节 > edit.html.erb
<%= form_with model: @chapter, url: { action: "update" } do |form| %>
...
<%= form.submit "Draft", class: "save-btn" %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)
路由文件
resources :books do
resources :chapters
end
Run Code Online (Sandbox Code Playgroud)
通常,我希望在更新/创建数据库中的章节的 POST …
我正在将Turbolinks 5与Rails 5一起使用,并且我正在设置一个 JS 间隔,以便通过 AJAX 每 10 秒刷新一次页面内容:
<script type="text/javascript">
$(document).ready(function() {
function fetchContent() {
// some AJAX request
}
setInterval(fetchContent, 10000);
});
</script>
Run Code Online (Sandbox Code Playgroud)
当我第一次加载包含上述代码的页面时,它可以工作:fetchContent每 10 秒触发一次。
但是,当我导航到另一个页面时,它fetchContent一直在触发。此外,当我返回该页面(使用浏览器后退按钮)时,fetchContent会不可预测地多次触发。显然,这不是想要的行为。
如何解决这个问题?
注意:要解决这个问题,我知道我可能需要某种clearInterval调用,但是我应该如何使用 Turbolinks 5 进行调用,例如我应该绑定什么事件?从 Turbolinks 5 开始,API 发生了变化。特别是,所有 Turbolinks Classic 事件都已重命名,并且有几个(包括)page:update已被删除,因此我无法使用诸如this、this或this 之类的解决方案。
ruby-on-rails setinterval turbolinks ruby-on-rails-5 turbolinks-5
我有一个使用 Turbolinks 的 Rails 应用程序。如果您不熟悉,turbolinks 会使document和window对象始终保持不变(页面永远不会刷新),并且它会通过 AJAX 拦截所有链接点击并替换 body 标记。
当在安装了 Vue 应用程序的页面之间导航时,这是我们观察到的内存/节点模式(它会增长到无穷大):
正如您所看到的,在每次页面更改时,内存只会增加,而不会被回收。
我们的应用程序使用https://github.com/jeffreyguenther/vue-turbolinks,基本上是这样的代码:
beforeMount: function() {
if (this === this.$root && this.$el) {
document.addEventListener('turbolinks:visit', function teardown() {
this.$destroy();
document.removeEventListener('turbolinks:visit', teardown);
})
// cache original element
this.$cachedHTML = this.$el.outerHTML;
// register root hook to restore original element on destroy
this.$once('hook:destroyed', function() {
if( this.$el.parentNode )
this.$el.outerHTML = this.$cachedHTML
});
}
}
Run Code Online (Sandbox Code Playgroud)
我在 Vue 应用程序上或其他任何地方都没有任何参考window(实例化代码只是new Vue({ options});没有全局变量,使用 const/let 的现代代码。
没有 …
我有一个使用Turbolinks和Backbone.js的Rails 4应用程序.
基本上,我的路由器看起来像:
var BookRouter = Backbone.Router.extend({
routes: {
'': 'index'
},
index: function(){
var bookView = new BookView({
el: ".wrapper"
});
}
});
$(function() {
if (Backbone.History.started == false) {
var bookRouter = new BookRouter();
Backbone.history.start({ pushState: true, hashChange: false });
}
});
$(document).on('page:load', function (){
Backbone.history.stop();
Backbone.history.start({ pushState: true, hashChange: false });
var bookRouter = new BookRouter();
});
Run Code Online (Sandbox Code Playgroud)
这是场景:
用户登陆http://website.com/,将用户带到索引路径,索引路径将模板加载到.wrapper中,如上面的路由代码所示.这很好用.
用户点击我的一个Rails生成的页面(例如,我有一条http://website.com/books路线显示用户创建的所有书籍.)这也可以.
用户仍然在http://website/books路线上.如果单击我的徽标,它会将您带到索引路径(由上面的骨干代码生成的路径).这很好用.
现在问题:如果你回到第3步,而不是点击徽标,决定点击后退按钮,然后从书籍路线回到索引路线,放入$ el .wrapper的内容是重复两次.例如,在我的模板中,我有"请选择要继续的书".如果你去了第1步,你只会看到一次这样的话.但是现在,每次进入/书籍然后点击后退按钮n次,你会看到n组单词.
我在rails项目上有以下JavaScript代码
// app/assets/javascript/simple_effects.js
$(function() {
// Hide completed tasks when checkbox is clicked. projects/show
$('#hide-completed').click(function() {
$('.task-completed').toggle();
});
// Change label to checkbox that hides completed tasks. project/show
$('#hide-completed').change(function(){
var c = this.checked ? ' Show completed tasks' : ' Hide completed tasks';
$('#checkbox-label').text(c);
});
});
Run Code Online (Sandbox Code Playgroud)
当我进入页面但仅在刷新之后,代码不会触发.我正在使用turbolinks.
我知道这个问题: Rails仅在页面重新加载后才加载我的javascript 但是我真的不知道是否可以在我的情况下使用该解决方案.谢谢!
编辑:这是我的application.js文件
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
Run Code Online (Sandbox Code Playgroud) 对于这个看似简单的问题,我找不到任何答案。在我的 rails5 应用程序中,某些链接确实使用 turbolinks 来加载新页面。但有些只是启动整页重新加载。
该链接没有附加 js,该链接上未禁用 turbolinks。
此链接使用 turbolinks:
= link_to qm_input_path('hour') do ...
<a class="navbar-brand" href="/qimen/hour/input"> ...
Run Code Online (Sandbox Code Playgroud)
在行动中它有
render layout: 'input'
Run Code Online (Sandbox Code Playgroud)
而这个链接没有:
= link_to qm_chart_path(@foo, @bar), class: 'btn btn-default pull-right next' do ...
<a class="btn btn-default pull-right next" href="/qimen/hour/chart/foo/bar"> ...
Run Code Online (Sandbox Code Playgroud)
在行动中它有
render layout: guess_layout
Run Code Online (Sandbox Code Playgroud)
因此,没有任何内容明确阻止 turbolinks 工作。
为什么?
turbolinks ×7
javascript ×3
turbolinks-5 ×3
jquery ×2
backbone.js ×1
hyperlink ×1
ruby ×1
setinterval ×1
vue.js ×1