Backbone.js路由器有多个页面应用?

时间:2013-09-16 14:37:19

标签: javascript backbone.js

我试图将backbone.js路由器与多页面应用程序一起使用。 路由器可以使用默认路由,即localhost,例如"http://localhost:35970/" 或www.myhompage.com“ 但我想在一个页面上使用骨干路由器,路径如下:

"http://localhost:35970/customer/index"

以便骨干路线看起来像这样“

"http://localhost:35970/customer/index#create"

但它似乎没有这种方式工作,有什么我想让它在这样的路径上工作吗?

这是我的代码,谢谢:

 var Router = Backbone.Router.extend({

        routes: {
            "Contact/Create": "create"

        },
        create: function () {
            alert('router test');
        }
    });

// EDIT

谢谢大家,这就是我所缺少的

root: "/customer/index/"

因此

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

我在没有指定根路径不是默认路径

的情况下启动了历史记录
Backbone.history.start({     pushState: true })

因此问题。

3 个答案:

答案 0 :(得分:2)

来自Backbone文档:

  

如果您的申请未通过您的根网址提供   域名,请务必告诉历史根目录的真实位置   选项:Backbone.history.start({pushState:true,root:   “/公共/搜索/”})

因此,您应该定义网址的哪个部分是根。在你的情况下:

Backbone.history.start({ 
    root: "/customer/index/"
})

答案 1 :(得分:0)

要使用路由器,您需要创建一个新对象,以便尝试:

var app_router = new Router();

然后你必须启动它,所以:

Backbone.history.start();

你现在应该可以去你的路线了。

骨干路由器是从一个路径到一个功能,所以在你的情况下,如果你去

http://localhost:35970/#Contact/Create

您应该看到'路由器测试'

答案 2 :(得分:0)

您需要定义根路径,即:

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

请参阅文档:http://backbonejs.org/#Router