Backbone.history.navigation with IE <= 9



使用主干历史/推送状态有问题-但只有在不支持它的浏览器(旧IE)

问题在于。当我第一次访问/en_gb/dashboard时,在所有浏览器中一切正常。然而,在IE<=9中,它将#dashboard附加到地址栏,形成/en_gb/dashboard#dashboard。现在,当我点击刷新时,我的路由器没有触发。

不是我所有的站点都在主干控制下-所以路由器正在工作:

routes: {
  'dashboard': 'showDashboard'
}

我的bootstrap是这样的:

if (Backbone.history) {
  var pushStateSupported = _.isFunction(history.pushState);
  var urlRoot = '/en_gb/';
  var enableSilent = !pushStateSupported;
  Backbone.history.start({
    pushState: pushStateSupported,
    root: urlRoot,
    silent: enableSilent
  });
  if (!pushStateSupported) {
    Backbone.history.navigate(window.location.pathname.substring(urlRoot.length), { trigger: true });
  }
}

添加调试,我可以看到Backbone.history.navigate()总是被调用,但当该哈希存在时,trigger: true似乎没有被拾取。

嗯-我似乎已经修复了它-虽然不是一个优雅的解决方案,这确实为我解决了它:

if (!pushStateSupported) {
    var route = window.location.pathname.substring(urlRoot.length);
    Backbone.history.navigate('/#' + route, { trigger: true });
}

地址栏中的URL显示为/en_gb/dashboard##dashboard,这不是很优雅,但它现在正在通过Backbone.navigate()方法。以前它在

上失败
if (this.fragment === fragment) return;

最新更新