From 1993ef56987f636b9305e3a541ffb2761927f935 Mon Sep 17 00:00:00 2001 From: Fabian Marz Date: Wed, 5 Jun 2019 14:12:07 +0200 Subject: [PATCH] Added use component default value if parent context key is not set. (#11) --- src/tags/index.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/tags/index.js b/src/tags/index.js index 772d297..5072a89 100644 --- a/src/tags/index.js +++ b/src/tags/index.js @@ -95,19 +95,23 @@ module.exports = function(fractal){ if (token.withStack !== undefined) { passedArguments = Twig.expression.parse.apply(this, [token.withStack, context]); - - _.forEach(passedArguments, function (value, name) { - // It makes no sense to pass variables that are not - // supported by the component. - if (innerContext[name] === undefined) { - return; - } - if (name.indexOf('attributes') > -1) { - innerContext[name].merge(value); - } - else { - innerContext[name] = value; - } + // Variables not defined by the component context are + // intentionally ignored. + _.forEach(innerContext, function (value, name) { + // Override default value only if an argument value is passed. + // Ignore undefined variables, which may appear when rendering + // a component with dummy/faker data but without values for its + // child components, so that each component only generates its + // own dummy data. + if (!passedArguments.hasOwnProperty(name) || typeof passedArguments[name] === 'undefined') { + return; + } + if (name.indexOf('attributes') > -1) { + value.merge(passedArguments[name]); + } + else { + innerContext[name] = passedArguments[name]; + } }); }