Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #25 from umco/patch/editor-value-fixes
Browse files Browse the repository at this point in the history
PropertyValueEditor - return converted value, not update reference value
  • Loading branch information
leekelleher authored Apr 11, 2018
2 parents 2da83ed + 3e85de8 commit 296f450
Showing 1 changed file with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,19 @@ public override string ConvertDbToString(Property property, PropertyType propert
return string.Empty;

// Process value
ConvertDbToStringRecursive(value, property, propertyType, dataTypeService);
ConvertDbToStringRecursive(value, dataTypeService);

// Update the value on the property
property.Value = JsonConvert.SerializeObject(value);

// Pass the call down
return base.ConvertDbToString(property, propertyType, dataTypeService);
// Return the serialized value
return JsonConvert.SerializeObject(value);
}

protected void ConvertDbToStringRecursive(JToken token, Property property, PropertyType propertyType, IDataTypeService dataTypeService)
protected void ConvertDbToStringRecursive(JToken token, IDataTypeService dataTypeService)
{
if (token is JArray jArr)
{
foreach (var item in jArr)
{
ConvertDbToStringRecursive(item, property, propertyType, dataTypeService);
ConvertDbToStringRecursive(item, dataTypeService);
}
}

Expand All @@ -60,7 +57,7 @@ protected void ConvertDbToStringRecursive(JToken token, Property property, Prope
{
if (kvp.Value is JArray || kvp.Value is JObject)
{
ConvertDbToStringRecursive(kvp.Value, property, propertyType, dataTypeService);
ConvertDbToStringRecursive(kvp.Value, dataTypeService);
}
}
}
Expand All @@ -82,22 +79,19 @@ public override object ConvertDbToEditor(Property property, PropertyType propert
return string.Empty;

// Process value
ConvertDbToEditorRecursive(value, property, propertyType, dataTypeService);
ConvertDbToEditorRecursive(value, dataTypeService);

// Update the value on the property
property.Value = JsonConvert.SerializeObject(value);

// Pass the call down
return base.ConvertDbToEditor(property, propertyType, dataTypeService);
// Return the JObject, Angular can handle it directly
return value;
}

protected void ConvertDbToEditorRecursive(JToken token, Property property, PropertyType propertyType, IDataTypeService dataTypeService)
protected void ConvertDbToEditorRecursive(JToken token, IDataTypeService dataTypeService)
{
if (token is JArray jArr)
{
foreach (var item in jArr)
{
ConvertDbToEditorRecursive(item, property, propertyType, dataTypeService);
ConvertDbToEditorRecursive(item, dataTypeService);
}
}

Expand All @@ -113,7 +107,7 @@ protected void ConvertDbToEditorRecursive(JToken token, Property property, Prope
{
if (kvp.Value is JArray || kvp.Value is JObject)
{
ConvertDbToEditorRecursive(kvp.Value, property, propertyType, dataTypeService);
ConvertDbToEditorRecursive(kvp.Value, dataTypeService);
}
}
}
Expand All @@ -123,27 +117,31 @@ protected void ConvertDbToEditorRecursive(JToken token, Property property, Prope
public override object ConvertEditorToDb(ContentPropertyData editorValue, object currentValue)
{
// Convert / validate value
if (editorValue.Value == null || string.IsNullOrWhiteSpace(editorValue.Value.ToString()))
return null;
if (editorValue.Value == null)
return string.Empty;

var value = JsonConvert.DeserializeObject<JToken>(editorValue.Value.ToString());
var dbValue = editorValue.Value.ToString();
if (string.IsNullOrWhiteSpace(dbValue))
return string.Empty;

var value = JsonConvert.DeserializeObject<JToken>(dbValue);
if (value == null || (value is JArray && ((JArray)value).Count == 0))
return null;
return string.Empty;

// Process value
ConvertEditorToDbRecursive(value, editorValue, currentValue);
ConvertEditorToDbRecursive(value, currentValue);

// Return value
return JsonConvert.SerializeObject(value);
}

protected void ConvertEditorToDbRecursive(JToken token, ContentPropertyData editorValue, object currentValue)
protected void ConvertEditorToDbRecursive(JToken token, object currentValue)
{
if (token is JArray jArr)
{
foreach (var item in jArr)
{
ConvertEditorToDbRecursive(item, editorValue, currentValue);
ConvertEditorToDbRecursive(item, currentValue);
}
}

Expand All @@ -159,7 +157,7 @@ protected void ConvertEditorToDbRecursive(JToken token, ContentPropertyData edit
{
if (kvp.Value is JArray || kvp.Value is JObject)
{
ConvertEditorToDbRecursive(kvp.Value, editorValue, currentValue);
ConvertEditorToDbRecursive(kvp.Value, currentValue);
}
}
}
Expand Down

0 comments on commit 296f450

Please sign in to comment.