Skip to content

Commit

Permalink
Merge pull request #1683 from riganti/fix-pagemodule-before-init
Browse files Browse the repository at this point in the history
Run module initialization in dotvvm.init event
  • Loading branch information
exyi authored Aug 3, 2023
2 parents af964d4 + c355c57 commit 29a3170
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ public ViewModuleInitResource(string[] referencedModules, string name, string vi
this.ReferencedModules = referencedModules.ToArray();
this.Dependencies = dependencies;

this.registrationScript = string.Join("\r\n", this.ReferencedModules.Select(m => $"dotvvm.viewModules.init({KnockoutHelper.MakeStringLiteral(m)}, {KnockoutHelper.MakeStringLiteral(viewId)}, document.body);"));
var initCalls = this.ReferencedModules.Select(m => $"dotvvm.viewModules.init({KnockoutHelper.MakeStringLiteral(m)}, {KnockoutHelper.MakeStringLiteral(viewId)}, document.body);");

// Run the module init in the init event
// * dotvvm.state will be available
// * executed before applying bindings to the controls, so the page module will initialize before control modules
this.registrationScript =
"dotvvm.events.init.subscribeOnce(() => {\n" +
" " + string.Join("\n", initCalls) + "\n" +
"})";
}

public void Render(IHtmlWriter writer, IDotvvmRequestContext context, string resourceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<script type="module">import * as m0 from '/dotvvmResource/myModule/myModule';dotvvm.viewModules.registerMany({'myModule': m0});</script>

<!-- Resource viewModule.init.6DLlOTYMqGV5yPAJoz5k_moKDBgEmZ3_HLLQ2zKDo74 of type ViewModuleInitResource. -->
<script type="module">dotvvm.viewModules.init("myModule", "p0", document.body);</script>
<script type="module">dotvvm.events.init.subscribeOnce(() => {
dotvvm.viewModules.init("myModule", "p0", document.body);
})</script>
</head>
<body>
<input id="__dot_viewmodel_root" type="hidden" value="{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
<script type="module">import * as m0 from '/dotvvmResource/viewModule/viewModule';dotvvm.viewModules.registerMany({'viewModule': m0});</script>

<!-- Resource viewModule.init.HIj399FcjVwbyaIvDKFctn7Ghr0UMajY-haUgZypcHs of type ViewModuleInitResource. -->
<script type="module">dotvvm.viewModules.init("viewModule", "p0", document.body);</script>
<script type="module">dotvvm.events.init.subscribeOnce(() => {
dotvvm.viewModules.init("viewModule", "p0", document.body);
})</script>
</head>
<body>
<div data-bind="dotvvm-with-view-modules: { modules: [&quot;controlModule&quot;] }">
Expand Down

0 comments on commit 29a3170

Please sign in to comment.