Można zdefiniować isAdmin
komputerowej nieruchomości w kontrolerze aplikacji:
// application controller
isAdmin: Ember.computed(function() {
// your logic here
})
// application template
{{#if isAdmin}}
{{admin-header}}
{{else}}
{{user-admin}}
{{/if}}
czy można owinąć go jako header-nav
komponentu z isAdmin
nieruchomości, więc:
// application template
{{header-nav isAdmin=isAdmin}}
UPDATE (szczegóły z Ember-prosty -auth for @Grimmy)
1) Wstrzykiwanie currentUser
i nto session
(np https://stackoverflow.com/a/30894082/4950029)
2) Rozwiąż currentUser
w beforeModel
haku i ustawić currentUser
własność Kontroler:
//route
beforeModel: function() {
var self = this;
this.session.get('currentUser').then(function(user) {
self.controllerFor(self.routeName).set('currentUser', user);
},function() {
self.controllerFor(self.routeName).set('currentUser', null);
});
}
//controller
isAdmin: Ember.computed('currentUser.role', function() {
return (this.get('currentUser.role') === 'admin');
}),
//template
{{#if isAdmin}}
{{admin-header}}
{{else}}
{{user-admin}}
{{/if}}
lub odpowiedział powyżej
//controller
roleBasedComponentName: Ember.computed('currentUser.role', function() {
return ((this.get('currentUser.role') === 'admin') ? 'admin-header' : 'user-header');
})
//template
{{component roleBasedComponentName user=currentUser}}
możesz dać mi pełną przykład kontrolera proszę? – Grimmy
Chcesz zobaczyć coś wyjątkowego? – artych
Użycie uwierzytelniania ember-simple-auth, chcę uzyskać bieżącą user.role, aby zdecydować, który widok wyrenderować – Grimmy