Forms can be bound to a structure just like other structures via the updater class.
Suppose we have an html form with some controls.
<div>
<form id="form1">
<input type="checkbox" name="checkbox">
<input type="text" name="text">
<input type="radio" name="radio">
<select name="select">
<option>value1</option>
<option>value2</option>
</select>
<textarea name="textarea">
</textarea>
<input type="button" name="button">
</form>
</div>
A new updater is created in this case via the following call.
const updater = new Updater(document.getElementById('form1'));
With the method Updater.getSubject()
you can get the structure of the updater. If you want to use an already defined structure, you can pass it to the updater as a second parameter.
let subject = updater.getSubject();
console.log(subject);
Now we want a click on the checkbox to be mapped in the data structure.
To do this we need to extend the html with the data-monster-bind
attribute.
The values or states of controls are mapped to the data structure via this binding.
<div>
<form id="form1">
<!-- data-monster-bind added -->
<input type="checkbox" name="checkbox" data-monster-bind="path:state">
</form>
</div>
In this case the status of the checkbox is mapped to the key state
. If the checkbox is selected the field state
contains the value on
, otherwise state is undefined.
If you want to use another value instead of on
, you can set the attribute value
.
<input type="checkbox" name="checkbox" value="checked" data-monster-bind="path:state">
To see the magic the handling must still be switched on.
updater.enableEventProcessing()
Voila!