mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-28 20:22:53 +00:00
New purpose for configMerge()
This commit is contained in:
parent
340a8e4176
commit
1f473228ce
45
js/module.js
45
js/module.js
@ -221,13 +221,14 @@ var Module = Class.extend({
|
|||||||
this.identifier = data.identifier;
|
this.identifier = data.identifier;
|
||||||
this.hidden = false;
|
this.hidden = false;
|
||||||
|
|
||||||
this.setConfig(data.config);
|
this.setConfig(data.config,data.configDeepMerge);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the module config and combine it with the module defaults.
|
* Set the module config and combine it with the module defaults.
|
||||||
*
|
*
|
||||||
* @param {object} config The combined module config.
|
* @param {object} config The combined module config.
|
||||||
|
* @param {boolean} config Merge module config in deep.
|
||||||
*/
|
*/
|
||||||
setConfig: function (config) {
|
setConfig: function (config) {
|
||||||
this.config = Object.assign({}, this.defaults, config);
|
this.config = Object.assign({}, this.defaults, config);
|
||||||
@ -440,6 +441,48 @@ var Module = Class.extend({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/** Merging MagicMirror (or other) default/config script
|
||||||
|
* merge 2 objects or/with array
|
||||||
|
* using:
|
||||||
|
* -------
|
||||||
|
* this.config = configMerge({}, this.defaults, this.config)
|
||||||
|
* -------
|
||||||
|
* arg1: initial objet
|
||||||
|
* arg2: config model
|
||||||
|
* arg3: config to merge
|
||||||
|
* -------
|
||||||
|
* why using it ?
|
||||||
|
* Object.assign() function don't to all job
|
||||||
|
* it don't merge all thing in deep
|
||||||
|
* -> object in object and array is not merging
|
||||||
|
* -------
|
||||||
|
* @bugsounet
|
||||||
|
* @Todo: idea of Mich determinate what do you want to merge or not
|
||||||
|
*/
|
||||||
|
|
||||||
|
function configMerge(result) {
|
||||||
|
var stack = Array.prototype.slice.call(arguments, 1);
|
||||||
|
var item;
|
||||||
|
var key;
|
||||||
|
while (stack.length) {
|
||||||
|
item = stack.shift();
|
||||||
|
for (key in item) {
|
||||||
|
if (item.hasOwnProperty(key)) {
|
||||||
|
if (typeof result[key] === "object" && result[key] && Object.prototype.toString.call(result[key]) !== "[object Array]") {
|
||||||
|
if (typeof item[key] === "object" && item[key] !== null) {
|
||||||
|
result[key] = configMerge({}, result[key], item[key]);
|
||||||
|
} else {
|
||||||
|
result[key] = item[key];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result[key] = item[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
Module.definitions = {};
|
Module.definitions = {};
|
||||||
|
|
||||||
Module.create = function (name) {
|
Module.create = function (name) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user