2

我需要从我的代码中的事件处理程序导航到另一个视图,我这样做

define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'plugins/router', 'underscore'],
 function (system, app, viewLocator, router, _) {
    system.log('starting app');
    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");
    app.title = 'Destiny';

    app.configurePlugins({
        router: true,
        dialog: true,
        widget: true,
        observable: true
    });

    router.map('destination', 'viewmodels/destination');
    router.activate();
    _.delay(function() {
        app.start().then(function() {
            //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
            //Look for partial views in a 'views' folder in the root.
            viewLocator.useConvention();

            //Show the app by setting the root view model for our application with a transition.
            app.setRoot('viewmodels/locationPicker', 'flip');
        });
    }, 1500);
}

);

在这里,我定义了我的 moudle 和路由器 - 目的地之间的映射,并将根视图设置为另一个视图 locationPicker

在我的另一个视图 locationPicker.js 中,我像这样导航到它

router.navigate('destination');

从开发人员工具中,我看到我的视图模型destination.js 文件加载没有错误,但视图根本没有改变。为什么会这样?顺便说一句,我使用的是 durandal 2.0.1 版本

4

1 回答 1

2

路由器插件在调用 app.start 时被初始化。因此,您在初始化之前配置了插件,并且配置没有被注册。另外,我不熟悉您注册路线的语法。更标准的方法是传入具有路由模式和模块 ID 的对象列表。请尝试以下方法:

define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'plugins/router'],
 function (system, app, viewLocator, router) {
    system.log('starting app');
    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");
    app.title = 'Destiny';

    app.configurePlugins({
        router: true,
        dialog: true,
        widget: true,
        observable: true
    });

    app.start().then(function() {

        router.map([
            { route: 'destination',  moduleId: 'viewmodels/destination' }
        ]).activate();

        //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
        //Look for partial views in a 'views' folder in the root.
        viewLocator.useConvention();

        //Show the app by setting the root view model for our application with a transition.
        app.setRoot('viewmodels/locationPicker', 'flip');
    });
}
于 2013-10-09T16:50:29.937 回答