开发者

jQuery deep copy with Ext JS?

开发者 https://www.devze.com 2023-02-17 12:24 出处:网络
I\'ve tried and surprised how could not I do with ExtJS. Let me explain with a code block. In jQuery console.clear();

I've tried and surprised how could not I do with ExtJS. Let me explain with a code block.

In jQuery

console.clear();
var a = {
    b: 5,
    c: 4,
    o: {
        l: 2,
        p: 2
    }
}

var b = {
    k: 4,
    l: 3,
    c: 5,
    o: {
        m: 2,
        l: 1
    }
}

var ex = $.extend(true, a, b);
console.dir(ex)

Here is the output

ex = {
    a: {
        q: 2
    },
    b: 5,
    c: 5,
    o: {
        l: 开发者_JS百科1,
        p: 2,
        m: 2
    }
}

Ext apply, applyIf, copyTo does not worked like this. How can I produce the output in ExtJS?

Thanks in advance.


For a recent project, we adapted this sample code to produce the following method:

Ext.deepCopy = function(p, c) {
    c = c || (p.constructor === Array ? [] : {});
    for (var i in p) {
        if (typeof p[i] === 'object' && p[i] !== null) {
            c[i] = p[i].constructor === Array ? [] : {};
            Ext.deepCopy(p[i], c[i]);
        } else {
            c[i] = p[i];
        }
    }
    return c;
};


Deep copying isn't supported in Ext. There are Ext.apply and Ext.applyIf but they both only work on the first level of a hash map and will override instead of merge any embedded arrays or hashes.

In fact the docs explicitly state that Ext.apply is meant to work on config objects, not that it matters but it's just to illustrate that it's not meant to be used as a merge utility function although it basically could if you only want to merge the first level/depth.


Use the Ext.Object.merge() method, that does exactly what you're looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消