Skip to content

Commit

Permalink
add reset fn
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbavykin1991 committed Oct 17, 2021
1 parent e461bff commit 4b9a7d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 7 additions & 2 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ class ValidatorClass {
constructor(owner, validate) {
this.instance = validate.get();

this.validate = async (...args) => {
this.validate = (...args) => {
const instance = validate(owner, ...args);
instance.done && instance.done((i) => (this.instance = i));
this.instance = instance;
};

this.reset = (...args) => {
validate.reset(...args);
this.instance = validate.get();
};
}

get errorCount() {
Expand All @@ -50,7 +55,7 @@ export function Validator(name, fn) {

willDestroy() {
super.willDestroy(...arguments);
validate.reset();
this.validator.reset();
}
};
};
Expand Down
7 changes: 4 additions & 3 deletions tests/dummy/app/components/groups-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Validator, test, enforce, only, group } from 'ember-vest';
}
});
})
export default class SimpleExampleComponent extends Component {
export default class GroupsExampleComponent extends Component {
@tracked type = 'sign_in';
@tracked email;
@tracked password;
Expand All @@ -40,10 +40,11 @@ export default class SimpleExampleComponent extends Component {

@action updateType(type, e) {
e.preventDefault();
const currentType = this.type;
this.type = type;

if (this.validator.instance.testCount) {
this.validator.validate();
if (currentType !== type) {
this.validator.reset();
}
}
}

0 comments on commit 4b9a7d9

Please sign in to comment.