【问题标题】:Backbone route on IEIE 上的主干路由
【发布时间】:2012-06-07 16:16:30
【问题描述】:

我正在尝试使用历史主干根目录,但它在 IE(或其他不支持历史 API 的浏览器)上无法正常工作。

我的网络应用有这张地图,其中每个模块发出一个请求,但actions应该调用一个函数

  • 站点/模块A/
  • 站点/模块A/action1/ID
  • 站点/模块B/
  • 站点/模块B/action1/ID

映射:

var MyRouter = Backbone.Router.extend({
    routes: {
        "moduleA/": "homeA",
        "moduleA/action1/:id": "action1", 
        // ...
    }
}

var app = new MyRouter();
Backbone.history.start({pushState: true});

我正在使用这个导航:

app.navigate('moduleA/',{trigger:true});

app.navigate('/moduleA/action1/4334',{trigger:true});

(我正在获取链接点击事件并调用 navigate(link.href,{trigger:true})

Every 在 Chr/FF(支持历史 API 的浏览器)上运行良好,并且 url 在浏览器中更新并且函数被调用。

但是,在 IE 中,url 被这种散列格式替换:site/#moduleA/ 为了解决这个问题,我尝试在 history.start 中设置 root

Backbone.history.start({pushState: true, root:'/moduleA/'});

但是,现在 IE 使用以下格式替换 url:site/moduleA/#moduleA/site/moduleA/#moduleA/action1/432432

那么,为什么 IE 在 url 中重复 root 呢? 我该如何解决这个问题?

提前致谢

【问题讨论】:

    标签: javascript backbone.js browser-history


    【解决方案1】:

    通过将root 设置为'/moduleA/',您告诉主干使用site/moduleA 作为根,这是预期的行为。

    记住骨干

    routes: {
        "moduleA/": "homeA",  // #moduleA/
        "moduleA/action1/:id": "action1" // #moduleA//action1/:id
    }
    

    不同于

    routes: {
        "/moduleA/": "homeA",  // #/moduleA/
        "/moduleA/action1/:id": "action1" // #/moduleA//action1/:id
    }
    

    在使用 app.navigate 时记住这一点很好。

    【讨论】:

    • 感谢安东尼提供的信息。我有很多错误。我必须更改所有链接并从 href 属性中删除 url 的根部分。另外,Backbone.history.start({pushState: true, root:'/moduleA/'});对于没有 html5 浏览器,必须有所不同:Backbone.history.start({pushState: true, root:'/moduleA'+(Modernizr.history?'/':'')});由于我正在设置根属性,它不必在路线中。在我的示例中,路由将是:{ '/':'home', 'action1/:id': 'action1'}
    猜你喜欢
    • 1970-01-01
    • 2016-08-27
    • 2012-08-17
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多