2015-06-07 06:19:19 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {Component, computed, isEmpty} = Ember;
|
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
/**
|
|
|
|
* Renders one random error message when passed a DS.Errors object
|
|
|
|
* and a property name. The message will be one of the ones associated with
|
|
|
|
* that specific property. If there are no errors associated with the property,
|
|
|
|
* nothing will be rendered.
|
|
|
|
* @param {DS.Errors} errors The DS.Errors object
|
|
|
|
* @param {string} property The property name
|
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Component.extend({
|
2015-06-07 06:19:19 +03:00
|
|
|
tagName: 'p',
|
|
|
|
classNames: ['response'],
|
|
|
|
|
|
|
|
errors: null,
|
|
|
|
property: '',
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
isVisible: computed.notEmpty('errors'),
|
2015-06-07 06:19:19 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
message: computed('errors.[]', 'property', function () {
|
|
|
|
let property = this.get('property');
|
|
|
|
let errors = this.get('errors');
|
|
|
|
let messages = [];
|
|
|
|
let index;
|
2015-06-07 06:19:19 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (!isEmpty(errors) && errors.get(property)) {
|
|
|
|
errors.get(property).forEach((error) => {
|
2015-06-07 06:19:19 +03:00
|
|
|
messages.push(error);
|
|
|
|
});
|
|
|
|
index = Math.floor(Math.random() * messages.length);
|
|
|
|
return messages[index].message;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|