A model is a special directive which can be used for two way binding on input element, which means - if you change the value in the code then UI will be automatically updated and vice versa.
👉 A model is shorthand for input
event and value
attribute.
<html>
<input type="text" :model(value) />
</html>
<script>
import {Component, reactive } from "mahal"
export default class NameComponent extends Component {
@reactive
value;
}
</script>
In the above code - if you will enter something into the input field, the script state will be automatically updated and in the same way if you will change the value of the state from code then input field value will be automatically updated.
Let's see how we can use model
for other input types
<html>
<div>
What is your super power ?
Can fly - <input type="checkbox" :model(canFly) />
Can run super fast - <input type="checkbox" :model(canRunSuperFast) />
Immortal - <input type="checkbox" :model(isImmortal) />
</div>
</html>
<script>
import {Component, reactive } from "mahal"
export default class NameComponent extends Component {
@reactive
canFly;
@reactive
canRunSuperFast;
@reactive
isImmortal;
}
</script>
In case of radio button -
<html>
<div>
<p>
<span>Male</span>
<input type="radio" :model(gender) name="gender" value="male" />
</p>
<p>
<span>Female</span>
<input type="radio" :model(gender) name="gender" value="female" />
</p>
</div>
</html>
<script>
import {Component, reactive } from "mahal"
export default class NameComponent extends Component {
@reactive
gender;
}
</script>