Skip to content

Commit

Permalink
add an afterRollback event
Browse files Browse the repository at this point in the history
  • Loading branch information
bgentry committed Jul 12, 2018
1 parent 353d0e5 commit b9662bd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
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.
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
7 changes: 5 additions & 2 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 @@ -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 @@ -1221,6 +1221,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

0 comments on commit b9662bd

Please sign in to comment.