Knockout在forEach和data之间切换模板

时间:2015-06-16 07:29:17

标签: javascript html knockout.js

我想要一个动态容器来显示我的所有内容,所以我有一个这样的模型:

function ContentViewmodel() {
        var engines = ko.observableArray(),
            application = ko.observable({ name: 'luna-content-engines', foreach: engines });

        function registerEngine(engine) {
            engines.push(engine);
        }

        function showConsoleContent() {
            application({ name: 'luna-console-template' });
        }

        function showEngineContent() {
            application({ name: 'luna-content-engines', foreach: engines });
        }

        return {
            registerEngine: registerEngine,
            engines: engines,
            showConsoleContent: showConsoleContent,
            showEngineContent: showEngineContent,
            application: application
        };

<div id="luna-content-container" data-bind="template: content"></div>
    <script type="text/html" id="luna-content">
        <div class="luna-content-menu">
            <div class="luna-content-menu-item" data-bind="click: showEngineContent">Engines</div>
            <div class="luna-content-menu-item" data-bind="click: showConsoleContent">Console</div>
        </div>
        <div>
            <div class="luna-content-engines-container" data-bind="template: application"></div>
        </div>
    </script>

如果我从默认设置切换到内容,它可以很好地工作,但切换回引擎,使用forEach不起作用;如果我将其更改回正常数据,它会再次起作用。

也许我对对象绑定做错了。任何建议,即使采用完全不同的方法?

0 个答案:

没有答案