Search code examples
objectextjsextenddeep-copy

jQuery deep copy with Ext JS?


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: 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.


Solution

  • 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.