2011-12-22 47 views
0

我發現ExtJS的Array存儲是如此緊密綁定,如果我想保留在一些臨時存儲中的原件,它也被修改。也就是說,如果我做這樣的事情:decoupling數組存儲

Ext.data.ArrayStores A,溫度

A = {}東西

溫度= A

Itemselector:商店= A

一.remove(有些記錄)

我發現Itemselector:商店是自動修改與A,這是我想要的但溫度也修改爲A被修改。我怎樣才能打破臨時和A之間的同步?

感謝

回答

-1

我猜你想要的是創建一個存儲和保持備份的拷貝在其原來的狀態?如果是這樣,你可以創建商店,然後保留它的數據更多的緩存。

Ext.data.ArrayStores A; 
//fill a with records 
var cacheOfAData = A.data;//Takes all the data in the store and caches it. 
A.remove(someRecords);//Remove records the cacheOfAData should still remain with the original data 

這只是這樣做的一種方式,還有更多的方法。

+0

你執行了這段代碼嗎? – 2011-12-22 19:00:24

1

這不是ExtJS數組商店,它是緊密結合的,它是Javascript!如果你搜索,你也會發現與其他框架類似的問題。並且在stackoverflow本身上有足夠的討論。

現在,這裏是您的解決方案通過一個例子:

var myStore = Ext.create('Ext.data.ArrayStore',{ 
    fields: [ 
     {name: 'fname',type: 'string'}, 
     {name: 'age', type: 'int'}  
    ], 
    data: [ 
     ['Sammy',28], 
     ['Steve', 31], 
     ['Albert', 30], 
     ['Abdel', 28], 
     ['Godwin',28] 
    ] 
}); 

var data = []; 
var dupStore = new Ext.data.Store({ 
     fields: [ 
      {name: 'fname',type: 'string'}, 
      {name: 'age', type: 'int'}  
     ] 
}); 

// Copy all records to a new array... 
myStore.each(function (rec){ 
    data.push (rec.copy()); 
});  

dupStore.loadRecords(data); // Your duplicate store 

注意我們是如何創建的,從的MyStore記錄的副本。像data = myStore.data這樣的簡單分配不會創建新的數組!

另一種方法是使用clone()創建一個對象。但據我所知,這也是一個淺的副本,因此將無法正常工作。