Reputation: 19
I have a problem with ember when I want to use a session data (companyId that is assigned to a user) in a model. I am using ember-cli and ember 0.13.1 with ember-simple-auth. Lets say I have a route called user/profile when I want to show information about company that the user is assigned to. In order to do that I need to retrieve a companyId that is assigned to a user from session and make a call with a model to my API. Currently the route for user/profile looks like that:
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
model: function() {
return Ember.RSVP.hash({
industries: this.store.findAll('industry'),
industryroles: this.store.findAll('industryrole'),
company: this.store.find('company',this.session.get('user.companyId'))
});
},
setupController: function(controller, model) {
controller.set('industries', model.industries);
controller.set('industryroles', model.industryroles);
controller.set('company', model.company);
}
});
Everything works when you go to that route from different route, but when you are in user/profile route and you press F5 then the this.session.get('user.companyId') is undefined.
Does somebody have an idea how I can solve that problem ?
Best regards Pawel
Upvotes: 0
Views: 937
Reputation: 19
If someone is still struggling with the problem described above. This is the solution:
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
model: function() {
return Ember.RSVP.hash({
industries: this.store.findAll('industry'),
industryroles: this.store.findAll('industryrole'),
company: this.store.find('company', this.get('session.data.user.companyId'))
});
},
setupController: function(controller, model) {
controller.set('industries', model.industries);
controller.set('industryroles', model.industryroles);
controller.set('company', model.company);
}
});
Upvotes: -1
Reputation: 19
I came up with some solution. I have set an observer for session.user.companyId in route of user/profile. So currently my route looks like that:
//app/routes/user/profile.js
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
onSessionIsAuthenticated: function () {
var isAuthenticated = this.get('session.isAuthenticated');
if (!isAuthenticated) {
return false;
}
var _this = this;
return Ember.RSVP.hash({
company: _this.store.find('company',this.get('session.user.companyId')).then(function (data) {
_this.controllerFor('user.profile').set('company', data);
})
});
}.observes('session.user.companyId'),
model: function() {
return Ember.RSVP.hash({
industries: this.store.findAll('industry'),
industryroles: this.store.findAll('industryrole')
});
},
setupController: function(controller, model) {
controller.set('industries', model.industries);
controller.set('industryroles', model.industryroles);
}
});
The rest industry and industry roles are still left in a controller. Now everything seems correctly but I am not sure if this is the correct solution for this problem. I am not sure if this is what you was in your mind @Microfed?
Upvotes: 0
Reputation: 2890
It's seems you need to look in session.content setting (if you not overriding beforeModel hook in user/profile route, of course).
If there is two step data fetching in your case, you need to wait for session data in user/profile model hook, and only then you can get user.companyId.
Upvotes: 0