Preventing full page reload on Backbone pushState

后端 未结 5 1005
夕颜
夕颜 2020-12-09 05:21

I\'m trying to prevent full page reloads using Backbone\'s pushState. When I call navigate() from my view\'s event, I see the messages marked // 1 below, but not // 2. In ad

5条回答
  •  春和景丽
    2020-12-09 05:56

    The answer is actually in here https://stackoverflow.com/a/9331734/985383, if you enable pushState you want links to work and not prevent them as suggested above, or well, is not just preventing them. here it is:

    initializeRouter: function () {
      Backbone.history.start({ pushState: true });
      $(document).on('click', 'a:not([data-bypass])', function (evt) {
    
        var href = $(this).attr('href');
        var protocol = this.protocol + '//';
    
        if (href.slice(protocol.length) !== protocol) {
          evt.preventDefault();
          app.router.navigate(href, true);
        }
      });
    }
    

提交回复
热议问题