ENH: Collections (update) (#40)

Fixes #36
This commit is contained in:
Matthew Ryan Dillon 2017-10-07 17:37:24 -07:00 committed by GitHub
parent e1abc5e4cb
commit f41f4caccd
17 changed files with 221 additions and 77 deletions

30
app/mixins/validation.js Normal file
View file

@ -0,0 +1,30 @@
import Ember from 'ember';
const { Mixin, get } = Ember;
const { keys } = Object;
export default Mixin.create({
validationSave(changeset, schema, postSave) {
return changeset
.cast(keys(schema))
.validate()
.then(() => {
if (changeset.get('isValid')) {
return changeset.save().then(postSave);
}
})
.catch((error) => {
/* eslint-disable no-console */
console.log(error);
/* eslint-enable no-console */
get(this, 'model.errors').forEach(({ attribute, message }) => {
changeset.pushErrors(attribute, message);
});
});
},
validationCancel(changeset, postCancel) {
changeset.rollback();
return postCancel();
},
});