Ember Route example for Testing Ember.js
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
App.NewPostRoute = Ember.Route.extend({ | |
model: function() { | |
return this.store.createRecord('post'); | |
}, | |
enter: function() { | |
if (!this.controllerFor('currentUser').get('isAdmin')) { | |
return this.transitionTo('posts'); | |
} | |
}, | |
renderTemplate: function() { | |
this.render('posts/new'); | |
return this.render('admin/go_back_button', { | |
outlet: 'navigation' | |
}); | |
}, | |
save: function() { | |
var self; | |
self = this; | |
return this.modelFor('newPost').save().then(function() { | |
self.controllerFor('newPost').clearErrors(); | |
self.redirectTo('admin.index'); | |
return Em.run.later(function() { | |
self.controllerFor('admin.index').set('showMessage', true); | |
return self.controllerFor('admin.index').set('message', 'Yay! the post has been created successfully!'); | |
}, 200); | |
}, function() { | |
self.controllerFor('admin.index').set('showMessage', true); | |
self.controllerFor('admin.index').set('isErrorMessage', true); | |
return self.controllerFor('admin.index').set('message', 'Oops! something went wrong!'); | |
}); | |
}, | |
rollBackAndRedirectTo: function(route) { | |
this.modelFor('newPost').rollback(); | |
return this.redirectTo(route); | |
}, | |
redirectTo: function(route) { | |
return this.transitionTo(route); | |
}, | |
actions: { | |
prepare: function() { | |
this.modelFor('newPost').set('status', 'draft'); | |
this.modelFor('newPost').set('author', this.controllerFor('currentUser').get('content')); | |
if (this.controllerFor('newPost').validates()) { | |
return this.save(); | |
} | |
}, | |
goBack: function() { | |
var shouldConfirm; | |
shouldConfirm = !this.controllerFor('newPost').get('isClear'); | |
if (shouldConfirm && confirm('All your changes will be lost, are you sure?')) { | |
this.rollBackAndRedirectTo('admin.index'); | |
} else if (!shouldConfirm) { | |
this.rollBackAndRedirectTo('admin.index'); | |
} | |
return this.controllerFor('newPost').clearErrors(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment