Selective history.back() using Backbone.js

后端 未结 2 1509
-上瘾入骨i
-上瘾入骨i 2021-02-06 02:33

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 u

2条回答
  •  醉梦人生
    2021-02-06 03:08

    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();
    

提交回复
热议问题