javascript 骨干路由 - 检测浏览器后退按钮按下

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10235509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:09:18  来源:igfitidea点击:

backbone routes – detecting browser back button press

javascriptbackbone.js

提问by samccone

I am trying to find a way to detect when the user has pressed the back / forward button in their browser.

我试图找到一种方法来检测用户何时按下了浏览器中的后退/前进按钮。

I am using backbone to handle routes, along with backbone to render my views.

我使用主干来处理路由,以及主干来呈现我的视图。

The issue is I can not hook into this page change event from anywhere.

问题是我无法从任何地方连接到此页面更改事件。

I tried putting a log in my initialize function for my views .. but it is not tripped when i use the back button.

我尝试在我的初始化函数中为我的视图添加一个日志......但是当我使用后退按钮时它没有被绊倒。

I am really not sure how else to detect this page change.

我真的不确定如何检测此页面更改。

采纳答案by Ph0en1x

You should use Backbone.Routerand make sure that you don't forget about Backbone.history.start()when starting your application.

您应该使用Backbone.Router并确保Backbone.history.start()在启动应用程序时不要忘记。

Some useful information can be found hereand here

一些有用的信息可以在这里这里找到

Actually the second link is not an article, but a blog. And the guy who wrote it really seems to know a lot about the topic.

其实第二个链接不是文章,而是博客。编写它的人似乎真的对这个主题了解很多。

UPD: The idea is that backbone's router fire binded methods when your url changing. So you just should put your logic to this method and this way you will able to track that. Maybe it's non very automatic way, and it will take to write some code but I think it should work.

UPD:这个想法是当你的 url 改变时,骨干的路由器会触发绑定方法。所以你应该把你的逻辑放到这个方法中,这样你就可以跟踪它。也许这不是非常自动的方式,需要编写一些代码,但我认为它应该可以工作。

回答by amypellegrini

You can bind a callback to Backbone routeevent:

您可以将回调绑定到 Backboneroute事件:

    Backbone.history.on('route', function () {
        // Do your stuff here
    });

You can use Backbone.history.getFragment()to know were you are standing.

你可以Backbone.history.getFragment()用来知道你是否站着。

回答by Claudiu

When the back button is pressed a popstateevent is triggered.

当按下后退按钮时,会触发popstate事件。

I'm using this piece of code:

我正在使用这段代码:

// listening for the browser's back button
window.addEventListener('popstate', function(e) {
    router.navigate(Backbone.history.getFragment(), { trigger: true, replace: true });
});

回答by Tronix117

you can extend the Backbone.Routerand overload the routeand the openmethod. You have all page changings handled in those two methods. So just recopy them from the backbone file, and play with it.

您可以扩展Backbone.Router和重载routeopen方法。您可以通过这两种方法处理所有页面更改。所以只需从主干文件中重新复制它们,并使用它。

回答by manuels

Obviously even GitHub does not know how to detect which button was pressed ;) They use this code:

显然,即使是 GitHub 也不知道如何检测按下了哪个按钮 ;) 他们使用以下代码:

slideTo: function (a) {
      var b = this.pathFromURL(a),
         c = this.frameForPath(b),
         d = $("#slider .frame-center").attr("data-path") || "";
      c.is(".frame-center") || (d == "/" || b.split("/").length > d.split("/").length ?
        this.slideForwardTo(a) : this.slideBackTo(a))
}