Skip to content

Instantly share code, notes, and snippets.

@efigarolam
Created November 13, 2013 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efigarolam/7450803 to your computer and use it in GitHub Desktop.
Save efigarolam/7450803 to your computer and use it in GitHub Desktop.
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