diff --git a/src/Framework/Framework/Compilation/Javascript/ParametrizedCode.cs b/src/Framework/Framework/Compilation/Javascript/ParametrizedCode.cs index cfc4b51940..e8306a9be5 100644 --- a/src/Framework/Framework/Compilation/Javascript/ParametrizedCode.cs +++ b/src/Framework/Framework/Compilation/Javascript/ParametrizedCode.cs @@ -146,20 +146,33 @@ public ParametrizedCode AssignParameters(Func - o == JavascriptTranslator.KnockoutContextParameter ? new ParametrizedCode("c") : - o == JavascriptTranslator.KnockoutViewModelParameter ? new ParametrizedCode("d") : + o == JavascriptTranslator.KnockoutContextParameter ? CodeParameterAssignment.FromIdentifier("c") : + o == JavascriptTranslator.KnockoutViewModelParameter ? CodeParameterAssignment.FromIdentifier("d") : default(CodeParameterAssignment) ); return new JsSymbolicParameter(new CodeSymbolicParameter("tmp symbol", defaultAssignment: adjustedCode)); diff --git a/src/Tests/Binding/BindingCompilationTests.cs b/src/Tests/Binding/BindingCompilationTests.cs index fdd0d6d360..1c7f2c6c8c 100755 --- a/src/Tests/Binding/BindingCompilationTests.cs +++ b/src/Tests/Binding/BindingCompilationTests.cs @@ -1493,7 +1493,7 @@ public override string ToString() return SomeString + ": " + MyProperty; } } - class TestViewModel3 : DotvvmViewModelBase + public class TestViewModel3 : DotvvmViewModelBase { public string SomeString { get; set; } } diff --git a/src/Tests/Binding/JavascriptCompilationTests.cs b/src/Tests/Binding/JavascriptCompilationTests.cs index 163022b480..c43ce56e2b 100644 --- a/src/Tests/Binding/JavascriptCompilationTests.cs +++ b/src/Tests/Binding/JavascriptCompilationTests.cs @@ -1434,6 +1434,40 @@ public void JavascriptCompilation_CustomPrimitiveParse() Assert.AreEqual("VehicleNumber()==\"123\"", result); } + [TestMethod] + public void JavascriptCompilation_ParametrizedCode_ParentContexts() + { + // root: TestViewModel + // parent: TestViewModel2 + _index + _collection + // this: TestViewModel3 + _index + var context0 = DataContextStack.Create(typeof(TestViewModel)); + var context1 = DataContextStack.Create(typeof(TestViewModel2), parent: context0, extensionParameters: [ new CurrentCollectionIndexExtensionParameter(), new BindingCollectionInfoExtensionParameter("_collection") ]); + var context2 = DataContextStack.Create(typeof(TestViewModel3), parent: context1, extensionParameters: [ new CurrentCollectionIndexExtensionParameter() ]); + + var result = bindingHelper.ValueBindingToParametrizedCode("_root.IntProp + _parent._index + _parent._collection.Index + _parent.MyProperty + _index + SomeString.Length", context2); + + Assert.AreEqual("$parents[1].IntProp() + $parentContext.$index() + $parentContext.$index() + $parent.MyProperty() + $index() + SomeString().length", result.ToDefaultString()); + + // assign `context` and `vm` variables + var formatted2 = result.ToString(o => + o == JavascriptTranslator.KnockoutContextParameter ? new ParametrizedCode("context", OperatorPrecedence.Max) : + o == JavascriptTranslator.KnockoutViewModelParameter ? new ParametrizedCode("vm", OperatorPrecedence.Max) : + default(CodeParameterAssignment) + ); + Console.WriteLine(formatted2); + + var symbolicParameters = result.Parameters.ToArray(); + Assert.AreEqual(6, symbolicParameters.Length); + Assert.AreEqual(2, XAssert.IsAssignableFrom(symbolicParameters[0].Parameter).ParentIndex); + Assert.AreEqual(JavascriptTranslator.KnockoutContextParameter, symbolicParameters[1].Parameter); // JavascriptTranslator.ParentKnockoutContextParameter would also work + Assert.AreEqual(JavascriptTranslator.KnockoutContextParameter, symbolicParameters[2].Parameter); // JavascriptTranslator.ParentKnockoutContextParameter would also work + Assert.AreEqual(JavascriptTranslator.ParentKnockoutViewModelParameter, symbolicParameters[3].Parameter); + Assert.AreEqual(JavascriptTranslator.KnockoutContextParameter, symbolicParameters[4].Parameter); + Assert.AreEqual(JavascriptTranslator.KnockoutViewModelParameter, symbolicParameters[5].Parameter); + + Assert.AreEqual("context.$parents[1].IntProp() + context.$parentContext.$index() + context.$parentContext.$index() + context.$parent.MyProperty() + context.$index() + vm.SomeString().length", formatted2); + } + public class TestMarkupControl: DotvvmMarkupControl { public string SomeProperty diff --git a/src/Tests/ControlTests/PostbackHandlerTests.cs b/src/Tests/ControlTests/PostbackHandlerTests.cs new file mode 100644 index 0000000000..478587a7eb --- /dev/null +++ b/src/Tests/ControlTests/PostbackHandlerTests.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Globalization; +using System.Threading.Tasks; +using CheckTestOutput; +using DotVVM.Framework.Compilation; +using DotVVM.Framework.Controls; +using DotVVM.Framework.Tests.Binding; +using DotVVM.Framework.ViewModel; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using DotVVM.Framework.Testing; +using System.Security.Claims; +using System.Collections; +using DotVVM.Framework.Binding; +using DotVVM.Framework.Compilation.ControlTree; +using DotVVM.Framework.Hosting; + +namespace DotVVM.Framework.Tests.ControlTests +{ + [TestClass] + public class PostbackHandlerTests + { + static readonly ControlTestHelper cth = new ControlTestHelper(config: config => { + }); + readonly OutputChecker check = new OutputChecker("testoutputs"); + + + [TestMethod] + public async Task ButtonHandlers() + { + var r = await cth.RunPage(typeof(BasicTestViewModel), """ + + + + 100 || SomeString.Length < 5} /> + + + + + + + + + + """ + ); + + check.CheckString(r.FormattedHtml, fileExtension: "html"); + } + + public class BasicTestViewModel: DotvvmViewModelBase + { + public int Integer { get; set; } = 123; + public bool Boolean { get; set; } = false; + public string String { get; set; } = "some-string"; + + public TestViewModel3 Nested { get; set; } = new TestViewModel3 { SomeString = "a" }; + } + } +} diff --git a/src/Tests/ControlTests/testoutputs/PostbackHandlerTests.ButtonHandlers.html b/src/Tests/ControlTests/testoutputs/PostbackHandlerTests.ButtonHandlers.html new file mode 100644 index 0000000000..08ca8d6fd0 --- /dev/null +++ b/src/Tests/ControlTests/testoutputs/PostbackHandlerTests.ButtonHandlers.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + diff --git a/src/Tests/ControlTests/testoutputs/ServerSideStyleTests.PostbackHandlers.html b/src/Tests/ControlTests/testoutputs/ServerSideStyleTests.PostbackHandlers.html index d2f57d3194..bc0ae66127 100644 --- a/src/Tests/ControlTests/testoutputs/ServerSideStyleTests.PostbackHandlers.html +++ b/src/Tests/ControlTests/testoutputs/ServerSideStyleTests.PostbackHandlers.html @@ -6,7 +6,7 @@ Two handlers Two handlers, value binding message - + Two handlers, resource binding message Two handlers, default message