Skip to content

Instantly share code, notes, and snippets.

@efigarolam
Created November 12, 2013 00: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/7423111 to your computer and use it in GitHub Desktop.
Save efigarolam/7423111 to your computer and use it in GitHub Desktop.
Ember Route example for Testing Ember.js
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