Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ember Controller example for Testing Ember.js
App.NewPostController = Ember.ObjectController.extend({
needs: ['currentUser'],
currentUser: Ember.computed.alias('controllers.currentUser'),
errors: [],
validates: function() {
var author, content, title;
title = this.get('content.title');
content = this.get('content.content');
author = this.get('content.author');
this.setErrors({
title: title,
content: content,
author: author
});
return !Em.isEmpty(title) && !Em.isEmpty(content) && !Em.isEmpty(author);
},
setErrors: function(properties) {
this.clearErrors();
if (Em.isEmpty(properties.title)) {
this.get('errors').pushObject('title');
}
if (Em.isEmpty(properties.content)) {
this.get('errors').pushObject('content');
}
if (Em.isEmpty(properties.author)) {
return this.get('errors').pushObject('author');
}
},
clearErrors: function() {
return this.get('errors').clear();
},
isErrored: (function() {
return !Em.isEmpty(this.get('errors'));
}).property('errors.@each'),
isASingularError: (function() {
return this.get('errors').length === 1;
}).property('errors.@each'),
isClear: (function() {
return Em.isEmpty(this.get('content.title')) && Em.isEmpty(this.get('content.content'));
}).property('content')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.