且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ExtJS 4:克隆商店

更新时间:2023-11-20 16:16:40

ExtJS 3.x解决方案



尝试这样:

  cloneStore:function(originStore,newStore){

if(!newStore){
newStore = Ext.create('Ext.data.Store',{
model:originStore模型
});
} else {
newStore.removeAll(true);
}

var records = [],originRecords = originStore.getRange(),i,newRecordData;
for(i = 0; i< originRecords.length; i ++){
newRecordData = Ext.ux.clone(originRecords [i] .copy()。
newStore.add(new newStore.model(newRecordData,newRecordData.id));
}

newStore.fireEvent('load',newStore);

return newStore;
}

注意: Ext.ux.clone 是一个分离的插件(你会发现它)使得一个对象的深层克隆。也许,Ext JS 4提供了一个熟悉的东西,我不知道..我使用这个特殊的克隆,因为Ext JS 3.x



有可能它需要在创建新商店时指定代理内存(现在我不确定,因为我始终使用提供的方式。



ExtJS 4.x解决方案

  function deepCloneStore ){
var target = Ext.create('Ext.data.Store',{
model:source.model
});

Ext.each source.getRange(),function(record){
var newRecordData = Ext.clone(record.copy()。data);
var model = new source.model(newRecordData,newRecordData.id);

target.add(model);
});

return target;
}
/ pre>

I'm trying to figure out how to clone an Ext.data.Store without keeping the old reference.

Let me explain better with some code. Here's the source store:

var source = Ext.create ('Ext.data.Store', {
    fields: ['name', 'age'] ,
    data: [
        {name: 'foo', age: 20} ,
        {name: 'boo', age: 30} ,
        {name: 'too', age: 10} ,
        {name: 'yoo', age: 80} ,
        {name: 'zoo', age: 30}
    ]
});

Follows an example of what I want to do:

var target = source;
target.removeAll ();
// Here I need to have target empty and source unchanged
// But in this case, source is empty as well

Now, in the above example the copy is done by reference while I need to do it by value. So I found Ext.clone () in the docs but it seems it doesn't work for complex object, like Ext.data.Store:

var target = Ext.clone (source);
target.removeAll ();
// source is still empty

Then I tried with Ext.data.Model.copy () but the only way to do it work is this:

var target = Ext.create ('Ext.data.Store', {
    fields: ['name', 'age']
});

source.each (function (model) {
    target.add (model.copy ());
});

Now, for my reasons, I don't want to instantiate another Ext.data.Store, so I want to avoid this:

var target = Ext.create ('Ext.data.Store', {
    fields: ['name', 'age']
});

I'd like to have something like this:

var target;

source.each (function (model) {
    target.add (model.copy ());
});

But, obviously, it doesn't work.

So, how can I clone the source store?

ExtJS 3.x solution

Try this:

cloneStore : function(originStore, newStore) {

    if (!newStore) {
        newStore = Ext.create('Ext.data.Store', {
            model : originStore.model
        });
    } else {
        newStore.removeAll(true);
    }

    var records = [], originRecords = originStore.getRange(), i, newRecordData;
    for (i = 0; i < originRecords.length; i++) {
        newRecordData = Ext.ux.clone(originRecords[i].copy().data);
        newStore.add(new newStore.model(newRecordData, newRecordData.id));
    }

    newStore.fireEvent('load', newStore);

    return newStore;
}

Note: Ext.ux.clone is a separated plugin (you will find it) which makes a deep clone of an object. Maybe, Ext JS 4 provides a familiar thing, I don't know.. I'm using this special clone since Ext JS 3.x

It is possible that it is required to specify the proxy memorywhen creating a new store (I'm not sure right now because I'm using always the "provided" way.

ExtJS 4.x solution

function deepCloneStore (source) {
    var target = Ext.create ('Ext.data.Store', {
        model: source.model
    });

    Ext.each (source.getRange (), function (record) {
        var newRecordData = Ext.clone (record.copy().data);
        var model = new source.model (newRecordData, newRecordData.id);

        target.add (model);
    });

    return target;
}