Durandal路由参数

时间:2014-07-04 10:42:34

标签: javascript jquery durandal

我有一个Durandal应用程序,其路由如下所示。

activate: function () {
    return router.map([{
        route: 'friends',
        moduleId: 'viewmodels/friends',
        nav: true
    }, {
        route: 'about',
        moduleId: 'viewmodels/about',
        nav: true
    }, {
        route: '(:id)',
        title: 'TimeLine',
        moduleId: 'viewmodels/Posts',
        nav: true
    }, ]).buildNavigationModel()
        .activate();

但每次我们导航到朋友'或者'关于'它转到了' viewmodel / Post'和朋友一起'和'关于'关键词是' id'。我不能去观看模特/朋友'和' viewmodels / about'。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

确保在viewmodels/中有一个名为posts.js的文件,而您的moduleId为moduleId: 'viewmodels/posts'而不是moduleId: 'viewmodels/Posts',而posts.js则必须相同< / p>

将您的代码更改为:

activate: function () {
    return router.map([{
        route: ['', 'friends'],//this is the home route ['', 'routeName']
        moduleId: 'viewmodels/friends',
        nav: true
    }, {
        route: 'about',
        moduleId: 'viewmodels/about',
        nav: true
    }, {
        route: 'posts/(:id)',// and change this from (:id) to posts/:id or posts/(:id)
        title: 'TimeLine',
        moduleId: 'viewmodels/posts',
        nav: true
    }, ]).buildNavigationModel()
        .activate();

在您的posts.js中,您可以像这样访问id's

var postId;
function activate(id) {
        postId = id;
        logger.log(viewModel.title + "'s  View Activated", null, viewModel.title, true);
        return true;
    }

function attached(view, parent) {
        alert(postId);
}

var viewModel = {
        activate: activate,
        title: 'singlePost',
        attached: attached
}

return viewModel;

详情请阅读Mapping Routes