Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an afterRollback event #292

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Be sure to call `validate()` on the `changeset` before saving or committing chan
* Events
+ [`beforeValidation`](#beforevalidation)
+ [`afterValidation`](#aftervalidation)
+ [`afterRollback`](#afterrollback)

#### `error`

Expand Down Expand Up @@ -743,6 +744,22 @@ changeset.validate().then(() => {

**[⬆️ back to top](#api)**

#### `afterRollback`

This event is triggered after a rollback of the changeset.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bgentry Would you mind adding a few sentences detailing the "use case" for users? To me it isn't immediately obvious, but I trust that there is a valid use case.

This can be used for [some advanced use cases](https://github.com/offirgolan/ember-changeset-cp-validations/issues/25#issuecomment-375855834)
where it is necessary to separately track all changes that are made to the changeset.

```js
changeset.on('afterRollback', () => {
console.log("changeset has rolled back");
});
changeset.rollback();
// console output: changeset has rolled back
```

**[⬆️ back to top](#api)**

## Validation signature

To use with your favorite validation library, you should create a custom `validator` action to be passed into the changeset:
Expand Down
9 changes: 6 additions & 3 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const OPTIONS = '_options';
const RUNNING_VALIDATIONS = '_runningValidations';
const BEFORE_VALIDATION_EVENT = 'beforeValidation';
const AFTER_VALIDATION_EVENT = 'afterValidation';
const AFTER_ROLLBACK_EVENT = 'afterRollback';
const defaultValidatorFn = () => true;
const defaultOptions = { skipValidate: false };

Expand Down Expand Up @@ -139,7 +140,7 @@ export type ChangesetDef = {|
_validateAndSet: <T>(string, T) => (Promise<T> | Promise<ErrLike<T>> | T | ErrLike<T>),
_setIsValidating: (string, boolean) => void,
_validate: (string, mixed, mixed) => (ValidationResult | Promise<ValidationResult>),
trigger: (string, string) => void,
trigger: (string, string | void) => void,
isValidating: (string | void) => boolean,
cast: (Array<string>) => ChangesetDef,
willDestroy: () => void,
Expand Down Expand Up @@ -388,14 +389,16 @@ export function changeset(
for (let key in relayCache) relayCache[key].rollback();

// Get keys before reset.
let keys = (this /*: ChangesetDef */)._rollbackKeys();
let c /*: ChangesetDef */ = this;
let keys = c._rollbackKeys();

// Reset.
set(this, RELAY_CACHE, {});
set(this, CHANGES, {});
set(this, ERRORS, {});
(this /*: ChangesetDef */)._notifyVirtualProperties(keys)
c._notifyVirtualProperties(keys)

c.trigger(AFTER_ROLLBACK_EVENT);
return this;
},

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,30 @@ test('afterValidation event is triggered with the key', function(assert) {
});
});

/**
* afterRollback
*/

test('afterRollback event is fired after rollback', function(assert) {
let dummyChangeset;
let _validator = () => resolve(true);
let _validations = {
reservations() {
return _validator();
}
};
let hasFired = false;

set(dummyModel, 'reservations', 'ABC12345');
dummyChangeset = new Changeset(dummyModel, _validator, _validations);
dummyChangeset.on('afterRollback', () => { hasFired = true; });

run(() => {
dummyChangeset.rollback();
assert.ok(hasFired, 'afterRollback should be triggered');
});
});

/**
* Behavior.
*/
Expand Down