Skip to content

Commit

Permalink
Expose changeset content (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
makepanic authored and nucleartide committed Feb 22, 2018
1 parent c58cd19 commit 9b305d9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Be sure to call `validate()` on the `changeset` before saving or committing chan
+ [`change`](#change)
+ [`errors`](#errors)
+ [`changes`](#changes)
+ [`data`](#data)
+ [`isValid`](#isvalid)
+ [`isInvalid`](#isinvalid)
+ [`isPristine`](#ispristine)
Expand Down Expand Up @@ -298,6 +299,19 @@ You can use this property to render a list of changes:

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

#### `data`

Returns the Object that was wrapped in the changeset.

```js
let user = { name: 'Bobby', age: 21, address: { zipCode: '10001' } };
let changeset = new Changeset(user);

changeset.get('data'); // user
```

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

#### `isValid`

Returns a Boolean value of the changeset's validity.
Expand Down
7 changes: 6 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ import {
isPresent,
typeOf,
} from '@ember/utils';
import { not } from '@ember/object/computed';
import {
not,
readOnly,
} from '@ember/object/computed';
import {
get,
set,
Expand Down Expand Up @@ -115,6 +118,7 @@ export type ChangesetDef = {|
errors: Array<{ key: string }>,
change: Inflated<mixed>,
error: Inflated<ErrLike<mixed>>,
data: Object,
isValid: boolean,
isPristine: boolean,
Expand Down Expand Up @@ -177,6 +181,7 @@ export function changeset(
errors: objectToArray(ERRORS, (e /*: Err */) => ({ value: e.value, validation: e.validation }), true),
change: inflate(CHANGES, c => c.value),
error: inflate(ERRORS, e => ({ value: e.value, validation: e.validation })),
data: readOnly(CONTENT),

isValid: isEmptyObject(ERRORS),
isPristine: isEmptyObject(CHANGES),
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ test('#change returns the changes object', function(assert) {
* #changes
*/

/**
* #data
*/

test("data reads the changeset CONTENT", function(assert) {
let dummyChangeset = new Changeset(dummyModel);

assert.equal(get(dummyChangeset, 'data'), dummyModel, 'should return data');
});

test("data is readonly", function(assert) {
let dummyChangeset = new Changeset(dummyModel);

assert.throws(
() => set(dummyChangeset, 'data', { foo: 'bar' }),
({message}) => message === "Cannot set read-only property 'data' on object: changeset:[object Object]",
'should throw error'
);
});

/**
* #isValid
*/
Expand Down

0 comments on commit 9b305d9

Please sign in to comment.