From 62b7fe7a16996cb389473a77c44e7d0b17eef3da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Standa=20Luke=C5=A1?= Date: Sat, 7 Oct 2023 15:39:13 +0200 Subject: [PATCH] datasets: fix generating client-side reload expression --- .../JavascriptTranslatableMethodCollection.cs | 2 +- .../Javascript/JsFormattingVisitor.cs | 2 +- .../GridViewDataSetBindingProvider.cs | 36 ++++--------------- src/Tests/ViewModel/GridViewDataSetTests.cs | 5 +-- 4 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/Framework/Framework/Compilation/Javascript/JavascriptTranslatableMethodCollection.cs b/src/Framework/Framework/Compilation/Javascript/JavascriptTranslatableMethodCollection.cs index d2bb464cb6..9c56f3ac5a 100644 --- a/src/Framework/Framework/Compilation/Javascript/JavascriptTranslatableMethodCollection.cs +++ b/src/Framework/Framework/Compilation/Javascript/JavascriptTranslatableMethodCollection.cs @@ -783,7 +783,7 @@ private void AddDataSetOptionsTranslations() { // GridViewDataSetBindingProvider AddMethodTranslator(typeof(GridViewDataSetBindingProvider), "DataSetClientSideLoad", new GenericMethodCompiler(args => - new JsIdentifierExpression("dotvvm").Member("dataSet").Member("loadDataSet").Invoke(args[1], args[2]))); + new JsIdentifierExpression("dotvvm").Member("dataSet").Member("loadDataSet").Invoke(args[1].WithAnnotation(ShouldBeObservableAnnotation.Instance), new JsSymbolicParameter(JavascriptTranslator.KnockoutContextParameter).Member("$gridViewDataSetHelper").Member("loadDataSet")).WithAnnotation(new ResultIsPromiseAnnotation(e => e)))); // PagingOptions AddMethodTranslator(() => default(PagingOptions)!.GoToFirstPage(),new GenericMethodCompiler(args => diff --git a/src/Framework/Framework/Compilation/Javascript/JsFormattingVisitor.cs b/src/Framework/Framework/Compilation/Javascript/JsFormattingVisitor.cs index 4a8c4a500e..4b3bd2af8b 100644 --- a/src/Framework/Framework/Compilation/Javascript/JsFormattingVisitor.cs +++ b/src/Framework/Framework/Compilation/Javascript/JsFormattingVisitor.cs @@ -192,7 +192,7 @@ public void VisitMemberAccessExpression(JsMemberAccessExpression memberAccessExp { EmitComment(memberAccessExpression.CommentBefore); if (!memberAccessExpression.MemberNameToken.IsValidName()) - new JsIndexerExpression(memberAccessExpression.Target.Clone(), new JsLiteral(memberAccessExpression.MemberNameToken)) + new JsIndexerExpression(memberAccessExpression.Target.Clone(), new JsLiteral(memberAccessExpression.MemberName)) .AcceptVisitor(this); else { diff --git a/src/Framework/Framework/Controls/GridViewDataSetBindingProvider.cs b/src/Framework/Framework/Controls/GridViewDataSetBindingProvider.cs index 7eafffe236..fda79be2db 100644 --- a/src/Framework/Framework/Controls/GridViewDataSetBindingProvider.cs +++ b/src/Framework/Framework/Controls/GridViewDataSetBindingProvider.cs @@ -145,7 +145,8 @@ private ICommandBinding CreateCommandBinding(GridViewDataSetC else if (commandType == GridViewDataSetCommandType.StaticCommand) { // on the client, wrap the call into client-side loading procedure - var expression = WrapInDataSetClientLoad(dataSetParam, body); + body.Add(CallClientSideLoad(dataSetParam)); + Expression expression = Expression.Block(body); if (transformExpression != null) { expression = transformExpression(expression); @@ -165,43 +166,20 @@ private ICommandBinding CreateCommandBinding(GridViewDataSetC } /// - /// Wraps the block expression { dataSet.XXXOptions.Method(); dataSet.RequestRefresh(); } - /// as loaderFunction => { ...; return GridViewDataSetBindingProvider.DataSetClientSideLoad(dataSet, loaderFunction); }); + /// Invoked the client-side loadDataSet function with the loader from $gridViewDataSetHelper /// - private static Expression WrapInDataSetClientLoad(Expression dataSetParam, List body) + private static Expression CallClientSideLoad(Expression dataSetParam) { - // get options and data set item type - var dataSetType = dataSetParam.Type; - var filteringOptionsConcreteType = GetOptionsConcreteType>(dataSetType, out _); - var sortingOptionsConcreteType = GetOptionsConcreteType>(dataSetType, out _); - var pagingOptionsConcreteType = GetOptionsConcreteType>(dataSetType, out _); - var dataSetItemType = dataSetType.GetInterfaces() - .Single(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IBaseGridViewDataSet<>)) - .GetGenericArguments()[0]; - - // resolve generic method and its parameter - var method = typeof(GridViewDataSetBindingProvider).GetMethod(nameof(DataSetClientSideLoad))! - .MakeGenericMethod(dataSetType, dataSetItemType, filteringOptionsConcreteType, sortingOptionsConcreteType, pagingOptionsConcreteType); - var loaderFunctionParam = Expression.Parameter(method.GetParameters().Single(p => p.Name == "loaderFunction").ParameterType, "loaderFn"); - // call static method DataSetClientLoad - var callClientLoad = Expression.Call(method, dataSetParam, loaderFunctionParam); - return Expression.Lambda(Expression.Block(body.Concat(new [] { callClientLoad })), loaderFunctionParam); + var method = typeof(GridViewDataSetBindingProvider).GetMethod(nameof(DataSetClientSideLoad))!; + return Expression.Call(method, dataSetParam); } /// /// A sentinel method which is translated to load the GridViewDataSet on the client side using the Load delegate. /// Do not call this method on the server. /// - public static Task DataSetClientSideLoad - ( - TGridViewDataSet dataSet, - Func, Task>> loaderFunction - ) - where TGridViewDataSet : IBaseGridViewDataSet, IFilterableGridViewDataSet, ISortableGridViewDataSet, IPageableGridViewDataSet - where TFilteringOptions : IFilteringOptions - where TSortingOptions : ISortingOptions - where TPagingOptions : IPagingOptions + public static Task DataSetClientSideLoad(IBaseGridViewDataSet dataSet) { throw new InvalidOperationException("This method cannot be called on the server!"); } diff --git a/src/Tests/ViewModel/GridViewDataSetTests.cs b/src/Tests/ViewModel/GridViewDataSetTests.cs index f9df671b88..9f10233881 100644 --- a/src/Tests/ViewModel/GridViewDataSetTests.cs +++ b/src/Tests/ViewModel/GridViewDataSetTests.cs @@ -124,6 +124,8 @@ public void GridViewDataSet_DataPagerCommands_StaticCommand() var commands = commandProvider.GetDataPagerCommands(dataContextStack, GridViewDataSetCommandType.StaticCommand); var goToFirstPage = CompileBinding(commands.GoToFirstPage); + Console.WriteLine(goToFirstPage); + XAssert.Equal("dotvvm.applyPostbackHandlers(async (options)=>{let vm=options.viewModel;dotvvm.dataSet.translations.PagingOptions.goToFirstPage(vm.PagingOptions);return await dotvvm.dataSet.loadDataSet(vm,options.knockoutContext.$gridViewDataSetHelper.loadDataSet);},this)", goToFirstPage); } private string CompileBinding(ICommandBinding staticCommand) @@ -134,8 +136,7 @@ private string CompileBinding(ICommandBinding staticCommand) new Literal(), new PostbackScriptOptions( allowPostbackHandlers: false, - returnValue: null, - commandArgs: CodeParameterAssignment.FromLiteral("commandArguments") + returnValue: null )); }