使用Backbone.js的选择性history.back() [英] Selective history.back() using Backbone.js

查看:130
本文介绍了使用Backbone.js的选择性history.back()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主干应用程序。我使用Backbone.history,以启用后退按钮。我们有一个页面(设置)是自动加载,要求用户输入一个弹出。如果用户选择取消,我想回去previous页面。我能做到这一点使用window.history.back()。

I have a Backbone app. I'm using Backbone.history to enable use of the back button. We have a page (settings) that auto loads a popup requiring input from the user. If the user chooses cancel, I want to go back to the previous page. I can do this using window.history.back().

问题是,如果用户通过键入网址到浏览器从另一个网址(如谷歌)就直接到该页面(应用#设置),我想将用户重定向到主页(应用程序/),而不是要回谷歌。

The problem is, if the user went directly to that page (app#settings) from another url (like google) by typing the url into the browser, I want to redirect the user to the home page (app/) rather than going back to google.

我一直没能想出什么办法来做到这一点。 Backbone.history看起来像它存储从浏览器的后退按钮的信息,因此具有即使他们刚刚到达的应用历史。我也无法找到一个方法来查看previous网址。

I haven't been able to figure out any way to do this. Backbone.history looks like it store information from the browser's back button, so it has a history even if they just arrived at the app. I also couldn't find a way to view the previous url.

这可能吗?

推荐答案

裹背导航逻辑在你自己的方法。也许在路由器上:

Wrap the back navigation logic in a method of your own. Perhaps on the router:

var AppRouter = Backbone.Router.extend({

  initialize: function() {
    this.routesHit = 0;
    //keep count of number of routes handled by your application
    Backbone.history.on('route', function() { this.routesHit++; }, this);
  },

  back: function() {
    if(this.routesHit > 1) {
      //more than one route hit -> user did not land to current page directly
      window.history.back();
    } else {
      //otherwise go to the home page. Use replaceState if available so
      //the navigation doesn't create an extra history entry
      this.navigate('app/', {trigger:true, replace:true});
    }
  }
});

和使用路由器的方法来导航回:

And use the router method to navigate back:

appRouter.back();

这篇关于使用Backbone.js的选择性history.back()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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