Skip to content

Commit

Permalink
Added helper methods for resource binding
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasherceg committed Aug 16, 2024
1 parent 3607ad9 commit f72d7fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/Framework/Framework/Binding/DotvvmBindingCacheHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ public ValueBindingExpression<TResult> CreateValueBinding<TResult>(string code,
}));
}

/// <summary> Compiles a new `{resource: ...code...}` binding which can be evaluated server-side. The result is cached. </summary>
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
}));
}

/// <summary> Compiles a new `{resource: ...code...}` binding which can be evaluated server-side. The result is implicitly converted to <typeparamref name="TResult" />. The result is cached. </summary>
public ResourceBindingExpression<TResult> CreateResourceBinding<TResult>(string code, DataContextStack dataContext, BindingParserOptions? parserOptions = null)
{
return CreateCachedBinding($"ResourceBinding<{typeof(TResult).ToCode()}>:{code}", new object?[] { dataContext, parserOptions }, () =>
new ResourceBindingExpression<TResult>(compilationService, new object?[] {
dataContext,
new OriginalStringBindingProperty(code),
parserOptions
}));
}

/// <summary> Compiles a new `{command: ...code...}` binding which can be evaluated server-side and also client-side. The result is cached. </summary>
public CommandBindingExpression CreateCommand(string code, DataContextStack dataContext, BindingParserOptions? parserOptions = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace DotVVM.Framework.Binding.Expressions
[Options]
public class ResourceBindingExpression : BindingExpression, IStaticValueBinding
{
public ResourceBindingExpression(BindingCompilationService service, IEnumerable<object> properties) : base(service, properties) { }
public ResourceBindingExpression(BindingCompilationService service, IEnumerable<object?> properties) : base(service, properties) { }

public BindingDelegate BindingDelegate => this.bindingDelegate.GetValueOrThrow(this);

Expand All @@ -34,7 +34,7 @@ public class OptionsAttribute : BindingCompilationOptionsAttribute

public class ResourceBindingExpression<T> : ResourceBindingExpression, IStaticValueBinding<T>
{
public ResourceBindingExpression(BindingCompilationService service, IEnumerable<object> properties) : base(service, properties) { }
public ResourceBindingExpression(BindingCompilationService service, IEnumerable<object?> properties) : base(service, properties) { }

public new BindingDelegate<T> BindingDelegate => base.BindingDelegate.ToGeneric<T>();
}
Expand Down

0 comments on commit f72d7fa

Please sign in to comment.