From f72d7faf032255eb1732e1024016aae6cc802e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herceg?= Date: Fri, 16 Aug 2024 17:23:00 +0200 Subject: [PATCH] Added helper methods for resource binding --- .../Binding/DotvvmBindingCacheHelper.cs | 22 +++++++++++++++++++ .../Expressions/ResourceBindingExpression.cs | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Framework/Framework/Binding/DotvvmBindingCacheHelper.cs b/src/Framework/Framework/Binding/DotvvmBindingCacheHelper.cs index b9e9c2c1eb..cda2f8c42b 100644 --- a/src/Framework/Framework/Binding/DotvvmBindingCacheHelper.cs +++ b/src/Framework/Framework/Binding/DotvvmBindingCacheHelper.cs @@ -60,6 +60,28 @@ public ValueBindingExpression CreateValueBinding(string code, })); } + /// Compiles a new `{resource: ...code...}` binding which can be evaluated server-side. The result is cached. + public ResourceBindingExpression CreateResourceBinding(string code, DataContextStack dataContext, BindingParserOptions? parserOptions = null) + { + return CreateCachedBinding("ResourceBinding:" + code, new object?[] { dataContext, parserOptions }, () => + new ResourceBindingExpression(compilationService, new object?[] { + dataContext, + new OriginalStringBindingProperty(code), + parserOptions + })); + } + + /// Compiles a new `{resource: ...code...}` binding which can be evaluated server-side. The result is implicitly converted to . The result is cached. + public ResourceBindingExpression CreateResourceBinding(string code, DataContextStack dataContext, BindingParserOptions? parserOptions = null) + { + return CreateCachedBinding($"ResourceBinding<{typeof(TResult).ToCode()}>:{code}", new object?[] { dataContext, parserOptions }, () => + new ResourceBindingExpression(compilationService, new object?[] { + dataContext, + new OriginalStringBindingProperty(code), + parserOptions + })); + } + /// Compiles a new `{command: ...code...}` binding which can be evaluated server-side and also client-side. The result is cached. public CommandBindingExpression CreateCommand(string code, DataContextStack dataContext, BindingParserOptions? parserOptions = null) { diff --git a/src/Framework/Framework/Binding/Expressions/ResourceBindingExpression.cs b/src/Framework/Framework/Binding/Expressions/ResourceBindingExpression.cs index 8273dffd56..fb5da85b8b 100644 --- a/src/Framework/Framework/Binding/Expressions/ResourceBindingExpression.cs +++ b/src/Framework/Framework/Binding/Expressions/ResourceBindingExpression.cs @@ -18,7 +18,7 @@ namespace DotVVM.Framework.Binding.Expressions [Options] public class ResourceBindingExpression : BindingExpression, IStaticValueBinding { - public ResourceBindingExpression(BindingCompilationService service, IEnumerable properties) : base(service, properties) { } + public ResourceBindingExpression(BindingCompilationService service, IEnumerable properties) : base(service, properties) { } public BindingDelegate BindingDelegate => this.bindingDelegate.GetValueOrThrow(this); @@ -34,7 +34,7 @@ public class OptionsAttribute : BindingCompilationOptionsAttribute public class ResourceBindingExpression : ResourceBindingExpression, IStaticValueBinding { - public ResourceBindingExpression(BindingCompilationService service, IEnumerable properties) : base(service, properties) { } + public ResourceBindingExpression(BindingCompilationService service, IEnumerable properties) : base(service, properties) { } public new BindingDelegate BindingDelegate => base.BindingDelegate.ToGeneric(); }