对于后退按钮骨干路由器越来越长的历史 [英] Backbone router getting history length for back-button

查看:171
本文介绍了对于后退按钮骨干路由器越来越长的历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PhoneGap的应用程序内骨干。像每一个移动应用程序,我需要一个后退按钮功能。它基本上完美地与骨干的工作,因为我可以简单地使用 window.history.back(),它就可以工作。

I'm using Backbone inside a PhoneGap application. Like every mobile app I need a back-button functionality. It's basically working perfectly with Backbone, because I can simply use window.history.back() and it just works.

我唯一的问题是决定何时显示后退按钮。使用 window.history.length 不起作用,因为使用时,它不会减少回()(当然,因为你可以去向前()以及)。

The only problem I have is to decide when to display the back button. Using window.history.length does not work, because it's not decremented when using back() (of course, because you can go forward() as well).

有什么方法来检测是否有更多的历史,或者如果我在堆栈的底部已经很?由于浏览器似乎并没有提供该信息(<一个href=\"http://stackoverflow.com/questions/3588315/how-to-check-if-the-user-can-go-back-in-browser-history-or-not\">How检查用户是否可以回去浏览器历史记录或不的)不骨干跟踪这个可能?

Is there any way to detect if there's more history or if I'm already at the bottom of the stack? Since the browser doesn't seem to supply this info (How to check if the user can go back in browser history or not) does Backbone keep track of this maybe?

推荐答案

凯西福斯特的回答看起来乍一看不错,但有一个缺陷:当使用 router.navigate 触发替换均设置为真正你最终了一个错误计数器。

Casey Foster's answer looks nice on first sight, but has a flaw: When using router.navigate with trigger and replace both set to true you'll end up with a wrong counter.

我想出了一个更简单的解决方案:基本上只有一种途径,我并不需要一个后退按钮,这就是首页路线。因此,我的code现在看起来像

I came up with a much simpler solution: Basically there's only one route where I don't need a back-button and that's the index route. Thus my code now looks like

Backbone.history.on('route', _.bind(function(router, route) {
    if(route === 'index') {
        this.$el.removeClass('has-back-button');
    } else {
        this.$el.addClass('has-back-button');
    }
}, this));

这篇关于对于后退按钮骨干路由器越来越长的历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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