1

使用 Backbone 历史/推送状态时遇到问题 - 但仅限于不支持它的浏览器(旧 IE)

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

并非我的所有站点都在 Backbone 控制之下 - 所以路由器正在工作:

routes: {
  'dashboard': 'showDashboard'
}

我的引导程序如下所示:

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没有被拾取。

4

1 回答 1

0

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

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;
于 2013-04-19T11:22:11.560 回答