[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.
This commit is contained in:
Victor Woeltjen 2014-12-03 15:19:23 -08:00
parent 363a6f6b47
commit d54524a96c

View File

@ -24,6 +24,24 @@ define(
propertyDefinition.conversion || "identity" 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, // Perform a lookup for a value from an object,
// which may recursively look at contained objects // which may recursively look at contained objects
// based on the path provided. // based on the path provided.
@ -78,11 +96,18 @@ define(
*/ */
getValue: function (model) { getValue: function (model) {
var property = propertyDefinition.property || var property = propertyDefinition.property ||
propertyDefinition.key; propertyDefinition.key,
initialValue =
property && lookupValue(model, property);
return property ? conversion.toFormValue( // Provide an empty array if this is a multi-item
lookupValue(model, property) // property.
) : undefined; if (Array.isArray(propertyDefinition.items)) {
initialValue = initialValue ||
new Array(propertyDefinition.items.length);
}
return conversion.toFormValue(initialValue);
}, },
/** /**
* Set a value associated with this property in * Set a value associated with this property in
@ -92,6 +117,13 @@ define(
var property = propertyDefinition.property || var property = propertyDefinition.property ||
propertyDefinition.key; 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); value = conversion.toModelValue(value);
return property ? return property ?