Durandal Routing:映射具有id的路由?

时间:2013-05-27 02:55:07

标签: knockout.js single-page-application durandal

我有以下路线:

 router.map([                
        { url: 'home', moduleId: 'viewmodels/home', name: 'Home', visible: false },               
        { url: 'my', moduleId: 'viewmodels/folder', name: 'My Content', visible: false }, // Should display contents of /folder/2
        { url: 'public', moduleId: 'viewmodels/folder', name: 'Public Content', visible: false }, // Should display contents of /folder/3
        { url: 'set/:id', moduleId: 'viewmodels/set', name: 'Set', visible: false },
        { url: 'folder/:id', moduleId: 'viewmodels/folder', name: 'Folder', visible: false }
    ]);

我想添加这样的内容:

{ url: 'test', moduleId: 'viewmodels/folder', id: '2', name: 'Specific Folder', visible: false }

导航到'test'显示me / folder / 2(但url保持/测试)。如何使用durandal路由器实现这一目标?

1 个答案:

答案 0 :(得分:2)

使用路线映射上的设置对象,如下所示:

{ 
    url: 'test',
    hash: '#/test',
    moduleId: 'viewmodels/folder',
    name: 'Specific Folder',
    visible: false,
    settings: {
        id: 2
    }
}

您可以在下面的视图模型中调试它(看起来Sammy实例作为参数传入):

define(function (require) {
    return {
        activate: activate
    };

    function activate(sammy) {
        var settings = sammy.routeInfo.settings;
        var id = settings.id;
        debugger;
    }
});

id的值应为2。

希望有所帮助,如果它没有尝试在documentation看一点:)