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

I am trying to apply the properties of an object from objA to objB, but I realized that Ext.apply is erroneous (or a blessing?) In such a way that it only 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 

An example outputs:

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

I would like to use this output (expected output):

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

How can I achieve this 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); 
+4
source share
2 answers

You can change the way Ext.apply () works, so that the 4th argument can be logical, implying a "deep" application - this will work with your example (deep defaults are true):

 Ext.apply = function(o, c, defaults, deep){ deep = deep!==false; // no "this" reference for 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; }; 
+1
source

You can try a recursive solution with a few special cases, as shown below:

 function deepApply(receiver, config, defaults) { if (defaults) { deepApply(receiver, defaults); } if (receiver && config && typeof config == 'object') { for (var p in config) { if (typeof config[p] != 'object' || Ext.isArray(config[p])) { receiver[p] = config[p]; } else { if (typeof receiver[p] != 'object') { receiver[p] = {}; } deepApply(receiver[p], config[p]); } } } return receiver; } 
0
source

Source: https://habr.com/ru/post/1342590/


All Articles