Skip to content

Commit

Permalink
Reusing the same part instances (#3310)
Browse files Browse the repository at this point in the history
Fixes #3305
  • Loading branch information
sebastienros authored Mar 13, 2019
1 parent 1cf77d9 commit ae34355
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement;
using System.ComponentModel.DataAnnotations;

namespace OrchardCore.Title.Model
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -9,6 +10,8 @@ namespace OrchardCore.ContentManagement
/// </summary>
public class ContentElement : IContent
{
internal Dictionary<string, ContentElement> _elements = new Dictionary<string, ContentElement>();

protected ContentElement() : this(new JObject())
{
}
Expand All @@ -33,8 +36,7 @@ protected ContentElement(JObject data)
/// <param name="name">The name of the property to look for.</param>
public bool Has(string name)
{
JToken value;
return Data.TryGetValue(name, out value);
return Data.ContainsKey(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static bool Has<TElement>(this ContentElement contentElement) where TElem
/// <returns>The content element instance or <code>null</code> if it doesn't exist.</returns>
public static ContentElement Get(this ContentElement contentElement, Type contentElementType, string name)
{
if (contentElement._elements.TryGetValue(name, out var element))
{
return element;
}

var elementData = contentElement.Data[name] as JObject;

if (elementData == null)
Expand All @@ -52,6 +57,8 @@ public static ContentElement Get(this ContentElement contentElement, Type conten
result.Data = elementData;
result.ContentItem = contentElement.ContentItem;

contentElement._elements[name] = result;

return result;
}

Expand All @@ -67,10 +74,11 @@ public static ContentElement Get(this ContentElement contentElement, Type conten

if (existing == null)
{
existing = new TElement();
existing.ContentItem = contentElement.ContentItem;
contentElement.Data[name] = existing.Data;
return existing;
var newElement = new TElement();
newElement.ContentItem = contentElement.ContentItem;
contentElement.Data[name] = newElement.Data;
contentElement._elements[name] = newElement;
return newElement;
}

return existing;
Expand All @@ -91,6 +99,7 @@ public static ContentElement Weld(this ContentElement contentElement, string nam
element.ContentItem = contentElement.ContentItem;

contentElement.Data[name] = element.Data;
contentElement._elements[name] = element;
}

return contentElement;
Expand Down

0 comments on commit ae34355

Please sign in to comment.