杜兰色组成foreach [英] Durandal composition foreach

查看:104
本文介绍了杜兰色组成foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历数组并通过合成显示其内容

I'm looping through an array and displaying its content through a composition

<!-- ko foreach: { data: datas} -->
    <div>
            <!-- ko compose: {
                model: 'show',
                activationData: {
                    bundle:bundle
                },                    
               } -->
            <!-- /ko -->
    </div>  
<!-- /ko -->

该合成调用了show.js模型,该模型将ActivationData捆绑包包装到一个可观察的对象中并显示在表格中

the composition calls a model show.js, that takes the activationData bundle wraps it in an observable and display it in a table

function (logger, utils) {
    var bundle = ko.observable();

    var activate = function (data) {
        // process data.bundle further 
        bundle(populateBundle(data.bundle));
    };
}

show.html

show.html

<table class="table table-bordered">
    <tbody>        
        <!-- ko foreach: {data:bundle().groups, as:'group'} -->
        <tr>
            <td class="tolerances-values" align="center"><span data-bind="text: group.name"></span></td>
            <td class="tolerances-values" align="center"><span data-bind="text: group.code"></span></td>

        </tr>
        <!-- /ko -->
    </tbody>
</table>

一切正常,但是当我遍历多个数据时,先前显示的所有合成都将显示最后一个元素的内容.我知道这与我将包用作可观察对象的事实有关,但是我如何才能让合成作品将发送到作品中的每个包视为孤立的可观察作品呢?我错过了什么

All works well, however when i'm looping though more than one data all the compositions displayed previously display the content of the last element. I know it's linked to the fact that i'm using bundle as an observable, how can i for the composition to treat every bundle sent to the composition as an isolated observable though ? Am I missing anything

推荐答案

原因是因为您没有在视图模型中使用构造函数.您需要创建一个对象,并将诸如Activate之类的内容附加到它的原型上,而不是您执行该操作的方式.在此处查看示例- http://durandaljs.com/documentation/Using-Composition.html

The reason is because you aren't using a constructor function inside of your view model. You need to create an object and attach things like activate onto it's prototype instead of the way that you are doing it. Check here for an example - http://durandaljs.com/documentation/Using-Composition.html

这是消息框文档中的另一个示例-

Here is another example from the message box docs -

var MessageBox = function(message, title, options) {
    this.message = message;
    this.title = title || MessageBox.defaultTitle;
    this.options = options || MessageBox.defaultOptions;
};

MessageBox.prototype.selectOption = function (dialogResult) {
    dialog.close(this, dialogResult);
};

这篇关于杜兰色组成foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆