无法绑定在shell.html和shell.js durandal中单击

时间:2013-11-01 10:09:49

标签: knockout.js durandal

这是我的源代码:

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()函数(我不知道为什么?)。 当我点击菜单时,它不执行此功能?请帮我解决这个问题。

1 个答案:

答案 0 :(得分: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时,您是否在控制台中出现任何错误?