ExtJS 4:克隆商店 [英] ExtJS 4: cloning stores

查看:154
本文介绍了ExtJS 4:克隆商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何克隆一个 Ext.data.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}
]
});

按照我想要做的一个例子:

  var target = source; 
target.removeAll();
//这里我需要有目标空和源不变
//但是在这种情况下,源也是空的,而

现在,在上面的例子中,复制是通过引用完成的,而我需要通过值来实现。
所以我在文档中发现了 Ext.clone(),但它似乎不适用于复杂对象,如 Ext.data .Store

  var target = Ext.clone(source); 
target.removeAll();
// source仍然是空的

然后我尝试使用 Ext .data.Model.copy()但唯一的方法是这样做:

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

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

现在,由于我的原因,我不想实例化另一个 Ext .data.Store ,所以我想避免这种情况:

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

我想要这样:

  var target; 

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

但显然,它不起作用。



那么,如何克隆源存储?

解决方案

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;
}

这篇关于ExtJS 4:克隆商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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