Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 836
Fatih Acet Avatar asked Mar 16 '11 22:03

Fatih Acet


1 Answers

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;
};
like image 177
Eli Courtwright Avatar answered Oct 21 '22 21:10

Eli Courtwright