Let’s say I have a user model, and a user has many permissions
App.User = DS.Model.extend({username: DS.attr('string'),permissions: DS.hasMany('permission', {async: true})});
App.Permission = DS.Model.extend({name: DS.attr('string')});
So I can get user permissions with the following:
user.get('permissions').forEach(function(p){console.log(p.get('name'));});
I want to check a user’s permissions before displaying a section of a template
{{#if has_permission(current_user, "edit_schedules")}}
"Button to edit schedules here"
{{/if}}
I’m having trouble with the has_permission funciton: the data I need to access is inside of a promise, so it can’t resolve right away:
function has_permission(user, permission_name){
user.get('permissions').foreach(function(p){
if (p.get('name') == permission_name){
// too late to return true here.
}
});
// callback hasn't yet fired here, don't know whether to return true or false.
}
What can I do instead to make this work?