Skip to content

Instantly share code, notes, and snippets.

@codeithuman
Created June 19, 2015 23:18
Show Gist options
  • Save codeithuman/6c3aec1528a0bff6bc99 to your computer and use it in GitHub Desktop.
Save codeithuman/6c3aec1528a0bff6bc99 to your computer and use it in GitHub Desktop.
try catch on saving ember object
import Ember from 'ember';
export default Ember.Controller.extend({
needs: ['question'],
actions: {
addAnswer: function() {
var self = this;
var newAnswer = self.store.createRecord('answer', {
author: self.get('author'),
date: new Date(),
body: self.get('body')
});
newAnswer.save().then(function(answer){
var currentQuestion = self.get('controllers.question.model');
currentQuestion.get('answers').pushObject(answer);
currentQuestion.save().then(function(q){
self.transitionToRoute('question', currentQuestion.id)
self.setProperties({
author: '',
date: '',
body: ''
});
}).catch(function(e){
console.log('Question did not save.'); //TODO: Handle error properly
});
}).catch(function(e){
console.log('answer did not save.'); //TODO: Handle error properly
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment