Created
November 27, 2015 20:42
-
-
Save ryrych/4a4167f80fc2c46f3151 to your computer and use it in GitHub Desktop.
After refactoring #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
var errorize = Ember.Object.extend({ | |
init() { | |
this._super(); | |
this.set('errorsList', {}); | |
}, | |
validObject() { | |
return !!this.get('errors'); | |
}, | |
buildErrorMessage(error) { | |
let field = this.getErrorField(error); | |
let message = error.detail; | |
this.addErrorMessage(field, message); | |
}, | |
getErrorField(error) { | |
// { source: { pointer:'/data/attributes/email' } } | |
return (/\w+$/.exec(error.source.pointer))[0].camelize(); | |
}, | |
addErrorMessage(field, message) { | |
(this.get('errorsList')[field] = this.get('errorsList')[field] || []) | |
.push({message}); | |
}, | |
serialize() { | |
if (!this.validObject()) { | |
return {}; | |
} | |
this.get('errors.responseJSON.errors').forEach(error => this.buildErrorMessage(error)); | |
return this.get('errorsList'); | |
}, | |
}); | |
export default errorize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment