2013-10-13 80 views
0

我在淘汰賽中遇到了一些關係問題。
在我的項目,我從constructionTypes到currentWindow.constructionParametres複製的項目,然後通過將項目從一個三維可觀察陣列複製到另一個

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

發生變化,但是它在constructionTypes太多變化。我試過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/

+0

我不清楚你想達到什麼目的。 *期望*輸出是什麼,爲什麼?你能展示你試過的各種解決方案,以及爲什麼/他們如何不工作?你是否正在尋找一些方法來深入複製項目(例如[this](http://api.jquery.com/clone/))? – Jeroen

+0

不是一個解決辦法,但你應該創建一個有那種關係,不同的ViewModels - 這將是至少可讀性和可維護性,即使它不解決您的問題。 – Ulflander

回答

0

Array.slice在你的情況下創建數組的一個副本,但陣列項目引用對象。因此複製數組的一部分並不是解決方案,因爲原始數據和複製數組中的項目將引用相同的對象。

您可以考慮使用內置的創建您的對象平原複製ko.toJS功能(平原意味着所有的觀測將成爲未看到)。也許這種方法不是你期望的,但它的工作原理:http://jsfiddle.net/ostgals/xveEP/73/

相關問題