diff --git a/README.md b/README.md index 8f354aea..2cfa41df 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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. diff --git a/addon/index.js b/addon/index.js index cde9ed8f..fc94e798 100644 --- a/addon/index.js +++ b/addon/index.js @@ -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, @@ -115,6 +118,7 @@ export type ChangesetDef = {| errors: Array<{ key: string }>, change: Inflated, error: Inflated>, + data: Object, isValid: boolean, isPristine: boolean, @@ -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), diff --git a/tests/unit/changeset-test.js b/tests/unit/changeset-test.js index b8470af1..981ab98a 100644 --- a/tests/unit/changeset-test.js +++ b/tests/unit/changeset-test.js @@ -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 */