From 8f9343aab1e9ea809835e69cc1277c167946564c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Standa=20Luke=C5=A1?= Date: Sun, 22 Oct 2023 15:32:04 +0200 Subject: [PATCH] Introduce DataContextStart.CreateCollectionElement Creates a nested data context with _index and _collection extension parameters --- .../GeneralBindingPropertyResolvers.cs | 5 ++--- .../ControlTree/DataContextStack.cs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Framework/Framework/Compilation/Binding/GeneralBindingPropertyResolvers.cs b/src/Framework/Framework/Compilation/Binding/GeneralBindingPropertyResolvers.cs index e0ffccd615..e29eb171f0 100644 --- a/src/Framework/Framework/Compilation/Binding/GeneralBindingPropertyResolvers.cs +++ b/src/Framework/Framework/Compilation/Binding/GeneralBindingPropertyResolvers.cs @@ -427,10 +427,9 @@ public ThisBindingProperty GetThisBinding(IBinding binding, DataContextStack sta public CollectionElementDataContextBindingProperty GetCollectionElementDataContext(DataContextStack dataContext, ResultTypeBindingProperty resultType) { - return new CollectionElementDataContextBindingProperty(DataContextStack.Create( + return new CollectionElementDataContextBindingProperty(DataContextStack.CreateCollectionElement( ReflectionUtils.GetEnumerableType(resultType.Type).NotNull(), - parent: dataContext, - extensionParameters: new CollectionElementDataContextChangeAttribute(0).GetExtensionParameters(new ResolvedTypeDescriptor(dataContext.DataContextType)).ToArray() + dataContext )); } diff --git a/src/Framework/Framework/Compilation/ControlTree/DataContextStack.cs b/src/Framework/Framework/Compilation/ControlTree/DataContextStack.cs index a655cadc35..f43a04f963 100644 --- a/src/Framework/Framework/Compilation/ControlTree/DataContextStack.cs +++ b/src/Framework/Framework/Compilation/ControlTree/DataContextStack.cs @@ -4,6 +4,7 @@ using System.Collections.Immutable; using System.Linq; using System.Runtime.CompilerServices; +using DotVVM.Framework.Binding; using DotVVM.Framework.Compilation.ControlTree.Resolved; using DotVVM.Framework.Utils; using FastExpressionCompiler; @@ -172,5 +173,23 @@ public static DataContextStack Create(Type type, var dcs = new DataContextStack(type, parent, imports, extensionParameters, bindingPropertyResolvers); return dcs;// internCache.GetValue(dcs, _ => dcs); } + + + /// Creates a new data context level with _index and _collection extension parameters. + public static DataContextStack CreateCollectionElement(Type elementType, + DataContextStack? parent = null, + IReadOnlyList? imports = null, + IReadOnlyList? extensionParameters = null, + IReadOnlyList? bindingPropertyResolvers = null) + { + var indexParameters = new CollectionElementDataContextChangeAttribute(0).GetExtensionParameters(new ResolvedTypeDescriptor(elementType.MakeArrayType())); + extensionParameters = extensionParameters is null ? indexParameters.ToArray() : extensionParameters.Concat(indexParameters).ToArray(); + return DataContextStack.Create( + elementType, parent, + imports: imports, + extensionParameters: extensionParameters, + bindingPropertyResolvers: bindingPropertyResolvers + ); + } } }