I’ve got a problem where a service that I need to be setup before all routes attempt to use it is not being setup fully before the routes.
In the service I have a user property that is set to null by default
user: null
When the service is initialized I call a function
init: function() {
this.getUser();
},
This getUser()
function looks up the user stored in local-storage using the local-storage-adapter found in this addon
getUser: function() {
let id = this.get('session.userId');
if (!isEmpty(id)) {
return this.get('store').findRecord('user', id).then((user)=>{
this.set('user',user);
this.notifyPropertyChange('user');
});
}
},
The session.userId
just stores the unique generated ID for the user object that was saved in local-storage at a previous time.
The issue I’m seeing is on the index route I’m attempting to lookup a model using the id of this user from the local-storage service but it’s retuning null. If I console log something in both locations I can see that in the index route where the user is still null, it’s being called before the .then()
in the getUser()
function is being returned. I’m sure this is a promise thing happening here, but I’m not sure what the solution is to make sure that all routes wait for this promise to be resolved before attempting to load data using properties of the user
property in the local-storage service.