2014-06-04 22:18:23 -05:00
|
|
|
import loadingIndicator from 'ghost/mixins/loading-indicator';
|
2014-06-13 14:35:22 -05:00
|
|
|
|
2014-07-25 08:38:13 -05:00
|
|
|
var PostsIndexRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, loadingIndicator, {
|
2014-09-09 20:22:11 -05:00
|
|
|
//Transition to posts.post if there are any posts the user can see
|
2014-06-17 14:17:08 -05:00
|
|
|
beforeModel: function () {
|
2014-07-31 03:29:05 -05:00
|
|
|
var self = this,
|
2014-07-21 09:13:52 -05:00
|
|
|
// the store has been populated so we can work with the local copy
|
2014-08-01 01:47:29 -05:00
|
|
|
posts = this.store.all('post'),
|
2014-09-09 20:22:11 -05:00
|
|
|
post;
|
2014-03-02 09:30:35 -05:00
|
|
|
|
2014-07-31 06:25:40 -05:00
|
|
|
return this.store.find('user', 'me').then(function (user) {
|
|
|
|
// return the first post find that matches the following criteria:
|
|
|
|
// * User is an author, and is the author of this post
|
|
|
|
// * User has a role other than author
|
2014-09-09 20:22:11 -05:00
|
|
|
post = posts.find(function (post) {
|
2014-07-31 06:25:40 -05:00
|
|
|
if (user.get('isAuthor')) {
|
|
|
|
return post.isAuthoredByUser(user);
|
|
|
|
} else {
|
|
|
|
return true;
|
2014-07-31 03:29:05 -05:00
|
|
|
}
|
|
|
|
});
|
2014-07-31 06:25:40 -05:00
|
|
|
if (post) {
|
|
|
|
return self.transitionTo('posts.post', post);
|
|
|
|
}
|
|
|
|
});
|
2014-03-02 09:30:35 -05:00
|
|
|
}
|
2014-03-03 15:18:10 -05:00
|
|
|
});
|
|
|
|
|
2014-07-31 06:25:40 -05:00
|
|
|
export default PostsIndexRoute;
|