是否可以为特定的jQuery Ajax调用禁用Turbolinks,以防止页面刷新和滚动? [英] Is it possible to disable Turbolinks for specific jQuery Ajax calls to prevent the page from refreshing and scrolling?

查看:82
本文介绍了是否可以为特定的jQuery Ajax调用禁用Turbolinks,以防止页面刷新和滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 5应用程序,非常想使用Turbolinks.

I have a Rails 5 application and would very much like to use Turbolinks.

在应用程序中,有多个PATCH ajax调用,它们仅用新数据更新服务器,而无需担心更新页面状态.

Within the application there are several PATCH ajax calls that simply update the server with new data, but do not need to worry about updating the state of the page.

每当这些ajax请求返回时,Turbolinks都会刷新页面,浏览器将滚动到屏幕顶部.这不是可取的行为.最好让事物保持原样.

Whenever these ajax requests return, Turbolinks refreshes the page and the browser scrolls to the top of the screen. This is not desirable behavior; it is much preferred that things just stay put where they are.

禁用Turbolinks可以消除此问题.

Disabling Turbolinks eliminates the problem.

导致问题的示例(超级基本)ajax调用:

Example (super basic) ajax call that causes the problem:

$.ajax({
  method: "PATCH",
  url: url,
  data: obj
});

是否有人遇到这种情况或对如何防止页面滚动发生有任何想法?

Is anyone else experiencing this or have any ideas on how to prevent the page scroll from occurring?

推荐答案

我在这里找到了答案: Github上的Turbolinks文档.

I found the answer here: Turbolinks documentation on Github.

第1步:将常规document.ready替换为自定义的turbolinks:load.

Step 1: replace the usual document.ready with a custom turbolinks:load.

第2步:确保指定类型为json

step 2: make sure to specify that the type is json

document.addEventListener('turbolinks:load', function() {
// your ajax code: 
    $.ajax({
        method: 'PATCH',
        url: url,
        dataType: 'json'
    })
}

这篇关于是否可以为特定的jQuery Ajax调用禁用Turbolinks,以防止页面刷新和滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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