I'm having a bit of trouble setting a property for my controller based off of a users attributes. Right now I have a simple set up where users can be admins of a group. I want to make it so if a user is an admin of a group, when they go to the groups/show page for that group, there is a form to create posts for that group, but this doesn't show up for regular members.
Right now my controller looks like this:
import Ember from 'ember';
export default Ember.Controller.extend({
loggedInUser: Ember.inject.service('user'),
isAdmin: Ember.computed(function() {
var that = this;
//getting the current user and the list of groups they are
//the admin of
this.get('loggedInUser').get('currentUser.administrating')
.then(function(groups) {
//getting the id of the current group
var groupID = that.get('group').get('id');
//looping over each group the user is an admin of
//and comparing the id to the current groups id
groups.forEach(function(group) {
if (group.get('id') == groupID) {
that.set('isAdmin', true);
}
})
});
})
});
The problem is this only works the first time. For example if a user visits a groups/show page for a certain group, and is an admin for that group, it will show the form just fine. However upon further visits to other groups/show pages for other groups, it will show the form, even if that user is not an admin for that group. I've done some digging and it appears the problem is that the controller is a singleton, and persists when navigating to other pages. I've using setupController in the route to set the isAdmin to false, however that just sets the property to false every time. Is there a proper way to do this?
Aucun commentaire:
Enregistrer un commentaire