getting store attr in a route emberjs

49 Views Asked by At

I'm fetching a user using:

var user = this.store.find('user', user_id);

here is the user model:

export default DS.Model.extend({
  username: DS.attr('string'),
  email: DS.attr('string'),
  first_name: DS.attr('string'),
  last_name: DS.attr('string'),
  password: DS.attr('string'),
  is_admin: DS.attr('boolean')
});

im trying to run a before model where i check if the user is an admin and then do something if its true or false, example:

beforeModel: function(){
    var user  = this.store.find('user', 1);

    if(user.get('is_admin')){
        return 'do something since they are an admin';
    }
},

I also tried doing this instead:

beforeModel: function(){
    var user  = this.store.find('user', 1);

    return user.then(function(response){
        if(response.is_admin){
            return 'do something since they are an admin';
        }
    });
},

How can I get that is_admin attribute in a route?

1

There are 1 best solutions below

0
On

You could define isAdmin property in your route, and set it when user promise resolved.

//route
isAdmin: false,
beforeModel: function() {
  var self = this; 
  return this.store.find('user', 1).then(function(user){
    self.set('isAdmin', user.get('is_admin'));
  }, function(){
    // on reject
  })
}