Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created September 19, 2018 02:23
Show Gist options
  • Save kalda341/234cd08e14aad5b5534134cf42d39607 to your computer and use it in GitHub Desktop.
Save kalda341/234cd08e14aad5b5534134cf42d39607 to your computer and use it in GitHub Desktop.
changeset: computed('_model', function() {
assert('validator must be set in siva-form', this.recordType);
let changeset = new Changeset(this._model, lookupValidator(this.validator), this.validator);
// ember-changeset, being the piece of shit that it is, doesn't consider something to be a 'change' if it
// doesn't validate. We want to know if there are unsaved changes, which we can't know if we don't keep track
// of it ourselves.
// List of {key, value}
this.set('changes', []);
let oldValidateAndSet = changeset._validateAndSet;
changeset._validateAndSet = (key, value) => {
let change = this.changes.find(change => change.key === key);
// If both are falsy then we can consider them equal, for every CURRENT case I can think of
if (isEqual(value, this._model[key]) || (!value && !this._model[key])) {
if (change) {
this.changes.removeObject(change);
}
} else if (change) {
change.set('value', value);
} else {
this.changes.pushObject(new EmberObject({key, value}));
}
// Call ember-changeset's function, the one we have overridden
oldValidateAndSet.bind(changeset)(key, value);
};
return changeset;
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment