首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >骨干pushState路由

骨干pushState路由
EN

Stack Overflow用户
提问于 2014-04-22 11:39:03
回答 1查看 236关注 0票数 3

为了更改漂亮urls上的哈希标签,我试图与backbone.js pushState打交道。使用localhost和我的脚本的路径是http://localhost/test/backbone/test.html。但是每次点击都会把我扔到localhost/login上。我做错了什么?

代码语言:javascript
复制
var AppRouter = Backbone.Router.extend({
    routes: {
        "login": "getPost",
        "*actions": "defaultRoute"
    }
});

var app_router = new AppRouter;

app_router.on('route:getPost', function (id) {
    alert( "login" );   
});

app_router.on('route:defaultRoute', function (actions) {
    alert( actions ); 
});

app_router.navigate("/login", {trigger: true});
Backbone.history.start({pushState: true, root: '/login/'});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-22 12:52:47

你需要添加:

代码语言:javascript
复制
$(document).delegate("a", "click", function(evt) {
    var href = $(this).attr("href");
    var protocol = this.protocol + "//";

    if (href.slice(protocol.length) !== protocol && protocol !== 'javascript://' &&      href.substring(0, 1) !== '#') {
        evt.preventDefault();

        Backbone.history.navigate(href, true);
    }
});

最后两个字符串应该是:

代码语言:javascript
复制
app_router.navigate("/login", {trigger: true}); // <- should remove this string
Backbone.history.start({pushState: true, root:"test/backbone/test.html"});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23218417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档