From d54524a96c520658263bd4768e3fae815dfc1cd8 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 3 Dec 2014 15:19:23 -0800 Subject: [PATCH] [Forms] Provide initial property arrays Provide initial empty arrays for type properties that are array-like; this is needed to support two-way data binding for composite controls. Part of ongoing integration of forms component, WTD-593. --- platform/core/src/types/TypeProperty.js | 40 ++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/platform/core/src/types/TypeProperty.js b/platform/core/src/types/TypeProperty.js index bf843f7891..55e4fb0a7b 100644 --- a/platform/core/src/types/TypeProperty.js +++ b/platform/core/src/types/TypeProperty.js @@ -24,6 +24,24 @@ define( propertyDefinition.conversion || "identity" ); + // Check if a value is defined; used to check if initial array + // values have been populated. + function isUnpopulatedArray(value) { + var i; + + if (!Array.isArray(value) || value.length === 0) { + return false; + } + + for (i = 0; i < value.length; i += 1) { + if (value[i] !== undefined) { + return false; + } + } + + return true; + } + // Perform a lookup for a value from an object, // which may recursively look at contained objects // based on the path provided. @@ -78,11 +96,18 @@ define( */ getValue: function (model) { var property = propertyDefinition.property || - propertyDefinition.key; + propertyDefinition.key, + initialValue = + property && lookupValue(model, property); - return property ? conversion.toFormValue( - lookupValue(model, property) - ) : undefined; + // Provide an empty array if this is a multi-item + // property. + if (Array.isArray(propertyDefinition.items)) { + initialValue = initialValue || + new Array(propertyDefinition.items.length); + } + + return conversion.toFormValue(initialValue); }, /** * Set a value associated with this property in @@ -92,6 +117,13 @@ define( var property = propertyDefinition.property || propertyDefinition.key; + // If an array contains all undefined values, treat it + // as undefined, to filter back out arrays for input + // that never got entered. + value = isUnpopulatedArray(value) ? undefined : value; + + // Convert to a value suitable for storage in the + // domain object's model value = conversion.toModelValue(value); return property ?