26
loading...
This website collects cookies to deliver better user experience
JavaScript.js
located in the wwwroot
folder with the following definitions:export default (contextApp) => new App(contextApp);
var WEB;
class App {
constructor(contextApp) {
this.contextApp = contextApp;
WEB = contextApp;
}
operation(a, b) {
let result = a + b;
WEB.namedCommands["ResultOperation"](result);
}
}
operation
function will be in charge of performing the sum and then put the result in the ResultOperation
section defined in a view with DotVVVM.App
class, and its defined context, we can now register it in DotVVM to be able to make calls from DotVVM to JavaScript, and vice versa. In this sense, we must go to the DotVVM Startup
class, and in the ConfigureResources
method refer to the JavaScript file in question, and name this record (in this case the name will be module
):private void ConfigureResources(DotvvmConfiguration config, string applicationPath)
{
//JS Directive
config.Resources.Register("module", new ScriptModuleResource(new UrlResourceLocation("~/JavaScript.js"))
{
//Dependencies = new[] { "external/other-js-file" }
});
}
DefaultViewModel.cs
):@viewModel JSDirective.ViewModels.DefaultViewModel, JSDirective
@masterPage Views/MasterPage.dotmaster
@js module
<dot:Content ContentPlaceHolderID="MainContent">
<div class="content">
<div class="content__text">
<h1>JS Directive Demo</h1>
<h3>Add two numbers</h3>
<ul class="content-page">
<li><b>Number1: </b></li>
<li><dot:TextBox Text="{value: Number1}" Type="Number" /></li>
<li><b>Number2: </b></li>
<li><dot:TextBox Text="{value: Number2}" Type="Number" /></li>
</ul>
<dot:Button Text="Calculate"
Click="{staticCommand:_js.Invoke("operation", Number1, Number2);}" />
<dot:NamedCommand Name="ResultOperation" Command="{staticCommand: (int id) => _root.Result = id}" />
<h3>Result: <b>{{value: Result}}</b></h3>
</div>
</div>
</dot:Content>
js
, with the name of the module previously registered:@js module
TextBox
have been used. Button
, specifying the name of the function, and sending the respective parameters (Number1
, and Number2
).NamedCommand
control allows us to invoke commands from JavaScript, in this case a section has been defined with the name ResultOperation, to send the value of the operation from JavaScript and save it in the variable Result
(defined in the ViewModel from DotVVM).