Durandal 子路由器仅更新绑定一次

Durandal child router updating bindings only once

我成功创建的路由器构建了一个导航模型,但是缺少更新一些绑定,每次在其中加载页面时都需要更新childRouterapp.pageTitleapp.pageDescription).

有没有什么方法可以将这些更新映射到 Durandal 的生命周期中,比如 activate 事件?

define([ 'durandal/app', 'plugins/router', 'knockout', 'app' ], function(app, router, ko, app) {

    console.log("content start");

    var childRouter = router.createChildRouter().makeRelative({
        moduleId : 'app/content/pages',
        fromParent : true
    }).map([ {
        route : [ '', 'grid' ],
        moduleId : 'grid/index'
    }, {
        route : 'details/:id',
        moduleId : 'details/index'
    }, {
        route : 'details/tabs/base',
        moduleId : 'details/tabs/base'
    } ]).buildNavigationModel();

    console.log("cms title start");
    app.pageTitle(app.i18n('app:modules.content.title'));
    app.pageDescription(app.i18n('app:modules.content.subtitle'));
    console.log("cms title stop");

    return {
        router : childRouter
    };
});

最简单的解决方案是添加 activate 函数并返回它。每次请求来自 childRouter 导航模型的页面时,Durandal 都会调用它。

define([ 'durandal/app', 'plugins/router', 'knockout', 'app' ], function(app, router, ko, app) {

    console.log("content start");

    var childRouter = router.createChildRouter().makeRelative({
        moduleId : 'app/content/pages',
        fromParent : true
    }).map([ {
        route : [ '', 'grid' ],
        moduleId : 'grid/index'
    }, {
        route : 'details/:id',
        moduleId : 'details/index'
    }, {
        route : 'details/tabs/base',
        moduleId : 'details/tabs/base'
    } ]).buildNavigationModel();

    function activate() {
        console.log("cms title start");
        app.pageTitle(app.i18n('app:modules.content.title'));
        app.pageDescription(app.i18n('app:modules.content.subtitle'));
        console.log("cms title stop");
    }        

    return {
        activate : activate,
        router : childRouter
    };
});