将项目从一个三维observablearray复制到另一个

时间:2013-10-13 08:44:49

标签: knockout.js knockout-2.0

我在淘汰赛中遇到一些关系问题 在我的项目中,我将itemTypes中的项目复制到currentWindow.constructionParametres,然后通过

进行更改
this.currentWindow().constructionParametres().items()[0].currentStep(2)

但它也在构造类型中发生了变化。我尝试过slice(),但没有成功 我该怎么办? 感谢。

function ReservationsViewModel() {

    this.constructionTypes = ko.observableArray([
        { id: 1, items: ko.observableArray([
            { type: 1, currentStep: ko.observable(1), steps: []},
            { type: 0, currentStep: ko.observable(1), steps: []},
            { type: 0, currentStep: ko.observable(1), steps: []},
            { type: 0, currentStep: ko.observable(1), steps: []}
        ])
        },
        { id: 2, items: ko.observableArray([
            { type: 1, currentStep: ko.observable(1), steps: []},
            { type: 2, currentStep: ko.observable(1), steps: []},
            { type: 0, currentStep: ko.observable(1), steps: []},
            { type: 0, currentStep: ko.observable(1), steps: []}
        ])
        }
    ]);     

    this.currentWindow = ko.observable({
        id: ko.observable(0),
        name: ko.observable('Need 1'),
        constructionParametres: ko.observable( this.constructionTypes().slice()[0] )
    });

    this.currentWindow().constructionParametres().items()[0].currentStep(2);
    this.currentWindow().constructionParametres().items()[0].currentStep(3);


}

ko.applyBindings(new ReservationsViewModel());

http://jsfiddle.net/xveEP/72/

1 个答案:

答案 0 :(得分:0)

在您的情况下,

Array.slice会创建一个数组副本,但数组项是引用到对象。因此,复制数组的一部分不是解决方案,因为原始数据集和复制数组中的项目将引用相同的对象。

您可以考虑使用内置ko.toJS函数来创建对象的 plain 副本( plain 意味着所有可观察对象都将变为不可观察的) 。也许这种方法不是你所期望的,但它有效:http://jsfiddle.net/ostgals/xveEP/73/

相关问题