BackboneJS用路由器更新URL?

时间:2013-11-22 02:55:27

标签: javascript backbone.js

我有这样的路由器

var Router= Backbone.Router.extend({
    routes : {
        "firstname/:name":"getfname",
        "lastname/:lname":"getlname",
        "age/:age":"getage",
        "*actions":"defaultRoute"
    }
})

当链接到他们时我想要他们

<a href="lastname/lastname>last name</a>
<a href="firstname/firstname>firstname</a>
<a href="lastname/lastname2>last name 2</a>

如何实现这一点所以当我点击lastname时,url将转到url.com/lastname/lastname然后当我点击firstname时它会转到url.com/lastname/lastname/firstname/firstname当我点击再次在第二个姓氏上,它会将url更新为lastname / lastname2 / firstname / firstname,保留url中的firstname参数。

1 个答案:

答案 0 :(得分:0)

您应该尝试使用pushState并定义根文件

Backbone.history.start({pushState: true, root: "/myapp/"})

另外,不要忘记正确使用引号

<a href="lastname/lastname">last name</a>
<a href="firstname/firstname">firstname</a>
<a href="lastname/lastname2">last name 2</a>