1

这是我的源代码:

main.js

requirejs.config({
    paths: {
        'text': '../Scripts/text',
        'transitions': '../Scripts/durandal/transitions',
        'durandal': '../Scripts/durandal',
        'plugins': '../Scripts/durandal/plugins',
        'knockout': '../Scripts/knockout-2.3.0',
        'jquery': '../Scripts/jquery-1.10.2.min',
        'nivoLightbox': '../Scripts/nivo-lightbox/nivo-lightbox'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'jQuery'
        }
    }
});

define(['durandal/system', 'durandal/app', 'durandal/viewLocator'], function (system, app, viewLocator) {
    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");
    app.title = 'Cu?i di ku';
    //specify which plugins to install and their configuration
    app.configurePlugins({
        router: true,
        dialog: true,
        widget: {
            kinds: ['expander']
        }
    });
    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.
        app.setRoot('shell', 'entrance');
    });
});

在我的shell.html,我有菜单:

<div id="top_nav" class="ddsmoothmenu">
    <ul>
        <li data-bind="click: goHome()"><a class="selected"></a></li>
    </ul>
    <br style="clear: left" />
</div>

shell.js

define(['plugins/router', 'durandal/app'], function (router, app) {
    return {
        router: router,
        activate: function () {
            router.map([
                { route: ['', 'home'], title: 'Mới nhất', moduleId: 'viewmodels/stories', nav: true },
                { route: ':storyId/:story', title: ':storyId', moduleId: 'viewmodels/story', nav: true }
            ]).buildNavigationModel();

            return router.activate();
        },
        goHome: function () {
            router.navigate('');
        }
    };
});

第一次加载页面时,它会运行goHome()(我不知道为什么?)。当我点击菜单时,它不执行这个功能?请帮我解决这个问题。

4

1 回答 1

2

尝试更换:

<li data-bind="click: goHome()"><a class="selected"></a></li>

with: (goHome 后没有 ())

<li data-bind="click: goHome"><a class="selected"></a></li>

() 在第一次应用绑定时调用 goHome 函数。

如果这仍然不能解决问题,当页面加载或单击 li 时,您是否在控制台中收到任何错误?

于 2013-11-01T10:21:21.807 回答