BY Simmi Kava
WebAtoms: WATCH & VALIDATE Decorators
WebAtoms Decorators - Watch & Validate
Watch Decorator
Watching property is super easy with Web Atoms. You can simply create a readonly accessor and mark it is @Watch
.
export default class EditorViewModel extends AtomViewModel {
public model: CustomerModel = {
firstName: "",
lastName: ""
};
@Watch
public get fullName() {
const firstName = this.model.firstName;
const lastName = this.model.lastName;
return firstName + " " + lastName;
}
}
Usage
this.render(<WA.AtomForm>
<WA.AtomField>
<XF.Entry text={Bind.twoWays(() =>
this.viewModel.model.firstName)}>
</WA.AtomField>
<WA.AtomField>
<XF.Entry text={Bind.twoWays(() =>
this.viewModel.model.lastName)}>
</WA.AtomField>
<WA.AtomField>
<XF.Label text={Bind.oneWay(() => this.viewModel.fullName)}>
</WA.AtomField>
</WA.AtomForm>);
Validate Decorator
Validate
is special type of watch, which gets activated only if validation was requested. You can simply mark the getter property as @Validate
.
export default class SignupViewModel extends AtomViewModel {
public model: CustomerModel = {
firstName: "",
lastName: "",
emailAddress: ""
};
@Validate
public get errorFirstName() {
if (!this.model.firstName) {
return "First name is required";
}
return "";
}
@Validate
public get errorEmailAddress() {
const email = this.model.emailAddress;
if (!email) {
return "Email is required";
}
if (!emailRegEx.test(email)) {
return "Invalid email";
}
return "";
}
@Action({ validate: true })
public async actionSignup() {
// only when this was invoked
// in UI, validation occurs
}
}
export default class Signup extends AtomXFContentPage {
public viewModel: SignupViewModel;
public create() {
this.viewModel = this.resolve(SignupViewModel);
this.render(<WA.AtomForm>
<WA.AtomField>
<XF.Entry text={Bind.twoWays(() =>
this.viewModel.model.firstName)}>
</WA.AtomField>
<WA.AtomField>
<XF.Entry text={Bind.twoWays(() =>
this.viewModel.model.emailAddress)}>
</WA.AtomField>
<WA.AtomField>
<XF.Button
command={Bind.event(() => this.viewModel.actionSignup())}
text="Signup">
</WA.AtomField>
</WA.AtomForm>);
}
}
BY Simmi Kava
Like | Comment | Save | Share |
ARCHIVES
2024
2023
2022
2021