class - Is there a better way to use Ext.apply to combine child/subobjects into another object? -


i'm trying apply object properties obja objb realised ext.apply flawed (or blessing?) in way applies first level objects together.

example:

var obja = {     name: 'obja',     baseparams: {         cols: [1,2,3,4,5]     } };  //used in subclass var objb = {     name: 'objb',     baseparams: {         limit: 50,         sort: 'name'     } };  //used in baseclass var objc = {     name: 'objc',     baseparams: {         as_hash: true,         limit: 20     } };  ext.apply(objb,obja); //used in subclass ext.apply(objc,objb); //used in baseclass 

example output:

obj = {     name: 'obja',     baseparams: {         cols: [1,2,3,4,5]     } }; 

i'd output instead (expected output):

obj = {     name: 'obja',     baseparams: {         cols: [1,2,3,4,5],         as_hash: true,         limit: 50,         sort: 'name'     } }; 

how can achieve without doing this?

// subclass: var bptemp = {}; bptemp.baseparams = ext.apply(objb.baseparams, obja.baseparams); ext.apply(objb,obja);  ext.apply(objb,bptemp);   // base class:  var bptemp = {}; bptemp.baseparams = ext.apply(objc.baseparams, objb.baseparams); ext.apply(objc,objb);  ext.apply(objc,bptemp);  

you can change way ext.apply() works 4th argument can boolean implying "deep" apply -- work example (deep defaults true):

ext.apply = function(o, c, defaults, deep){     deep = deep!==false;     // no "this" reference friendly out of scope calls     if(defaults){         ext.apply(o, defaults);     }     if(o && c && typeof c == 'object'){         for(var p in c){             o[p] = (deep && ext.isobject(o[p]) && ext.isobject(c[p])) ? ext.apply(o[p], c[p]) : c[p];         }     }     return o; };  

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -