В моем приложении мне нужно пользователям иметь доступ к организациям в соответствии с многие ко-многим, как это:Ember SimpleAuth isAuthorised на itemController
App.User = DS.Model.extend({
fullname: DS.attr('string'),
password: DS.attr('string'),
administrator: DS.attr('boolean'),
organisation_users: DS.hasMany('organisation_user', {async: true})
});
App.OrganisationUser = DS.Model.extend({
organisation: DS.belongsTo('organisation', {async: true}),
user: DS.belongsTo('user', {async: true}),
administrator: DS.attr('boolean')
});
App.Organisation = DS.Model.extend({
fullname: DS.attr('string', {defaultValue: 'Unnamed University'}),
description: DS.attr('string'),
organisation_users: DS.hasMany('organisation_user', {asynch: false}),
});
Я использую Ember SimpleAuth для аутентификации. Так на основе this answer to a previous question я реализовал isAuthorised на моем OrganisationController так:
App.OrganisationController = Ember.Controller.extend({
//Determine whether the logged in user is authorised
isAuthorised: function() {
if(!this.get('session.isAuthenticated')) {
console.log("Not logged in...");
return false;
}
console.log("Does user [" + this.get('session.user.id') + "] have access to organisation [" + this.get('model.id') + "]?");
if(this.get('session.user.administrator')) {
console.log("Yes, because they are an administrator.");
return true;
} else {
console.log("Well, they are not an administrator so...");
}
console.log("We need to check " + this.get('model.fullname') + " for authorised users.");
this.get('model.organisation_users').forEach(function(org_user){
org_user.get('user').then(function(user) {
console.log(user.get('fullname') + " has access");
});
});
}.property('model', 'session.user'),
Проблема заключается в том, я не знаю, как вернуть значение из этого. Кроме того, когда я загружаю OrganisationRoute, кажется, работает нормально, но когда я загрузить другой маршрут и переход на этот маршрут, он терпит неудачу с
Uncaught TypeError: Cannot read property 'resolve' of undefined
и
Uncaught Error: Something you did caused a view to re-render after it rendered but before it was inserted into the DOM.
Можете ли вы показать, как вы выполняете загрузку (api json, маршруты, init и т. Д.)? Похоже, что 'org_user.get ('user')' возвращается как пустое свойство, а внутренности '. Then' - barfing. Просто догадка. Попробуйте прокомментировать это и посмотрите, все ли все еще взрывается. –