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

#unexecute to revert changes to underlying Object from #save #567

Merged
merged 5 commits into from
Dec 7, 2020
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
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ name: CI

on:
push:
pull_request:
branches:
- master
pull_request: {}

jobs:

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Some notable changes going from 2.x to 3.x
- error.validation is a string if one validation and array if multiple validations.
- Defining a validation for a nested key worked before with {'something.else': ValidationFunc} and now it only works when defining as an object: { something: { else: validationFunc } }.

## [3.10.4](https://github.com/poteto/ember-changeset/tree/v3.10.4) (2020-12-05)

- #unexecute to revert changes to underlying Object from #save

## [3.10.3](https://github.com/poteto/ember-changeset/tree/v3.10.3) (2020-12-05)

- Support multiple validators with class validators
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ import { ValidationResult, ValidatorMapFunc, ValidatorAction } from 'ember-chang
- [`set`](#set)
- [`prepare`](#prepare)
- [`execute`](#execute)
- [`unexecute`](#unexecute)
- [`save`](#save)
- [`merge`](#merge)
- [`rollback`](#rollback)
Expand Down Expand Up @@ -577,6 +578,21 @@ Note that executing the changeset will not remove the internal list of changes -

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

#### `unexecute`

Undo changes made to underlying Object for changeset. This is often useful if you want to remove changes from underlying Object if `save` fails.

```js
changeset
.save()
.catch(() => {
// save applies changes to the underlying Object via this.execute(). This may be undesired for your use case.
dummyChangeset.unexecute();
})
```

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

#### `save`

Executes changes, then proxies to the underlying Object's `save` method, if one exists. If it does, the method can either return a `Promise` or a non-`Promise` value. Either way, the changeset's `save` method will return
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@glimmer/tracking": "^1.0.1",
"ember-auto-import": "^1.5.2",
"ember-cli-babel": "^7.19.0",
"validated-changeset": "~0.10.3"
"validated-changeset": "~0.10.4"
},
"devDependencies": {
"@ember/optional-features": "^1.0.0",
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,7 @@ module('Unit | Utility | changeset', function (hooks) {
await dummyChangeset.save();
assert.ok(false, 'WAT?!');
} catch (error) {
dummyChangeset.unexecute();
assert.equal(error.message, 'some ember data error');
} finally {
assert.equal(dummyModel.name, undefined, 'old name');
Expand All @@ -1409,6 +1410,7 @@ module('Unit | Utility | changeset', function (hooks) {
await dummyChangeset.save();
assert.ok(false, 'WAT?!');
} catch (error) {
dummyChangeset.unexecute();
assert.equal(error.message, 'some ember data error');
} finally {
assert.equal(dummyModel.name, 'original', 'old name');
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13497,10 +13497,10 @@ validate-npm-package-name@~2.2.2:
dependencies:
builtins "0.0.7"

validated-changeset@~0.10.3:
version "0.10.3"
resolved "https://registry.yarnpkg.com/validated-changeset/-/validated-changeset-0.10.3.tgz#5f1280bdf6f6dd9cfecd404362c5154067ee21c8"
integrity sha512-qTvbPVtHO7j6ZZ14UxmzlZPgcNnaruFbaZnHwvNPwNYqWDH3Xkj3Fe1h20Bb/XwOFrqAOjc/iXs4OY3mMEcySg==
validated-changeset@~0.10.4:
version "0.10.4"
resolved "https://registry.yarnpkg.com/validated-changeset/-/validated-changeset-0.10.4.tgz#dcb3961f4e974eeb9430804f657b6f8eae84d5dd"
integrity sha512-05opaR2iE5XohmARGnMM04lx7xIQDiT3wxkvAQrT7Ix7EhmAHSWkFZbigoQ6iSLyERAaUw5IL593a5rUWkyFQg==

vary@~1.1.2:
version "1.1.2"
Expand Down