简体   繁体   中英

Promise in Ember route model not resolving/updating

I'm using Ember Simple Auth and a service that gets injected into application controller to keep track of currently logged in user. I can use {{accountName}} for the currently logged in user in my application template by doing the following:

//controllers/applications.js
import Ember from 'ember';

export default Ember.Controller.extend({
  session: Ember.inject.service(),
  userFromSession: Ember.inject.service('session-user'),
  accountName: Ember.computed('session.data.authenticated.userId', function(){
    this.get('userFromSession.user').then((user)=>{
      if (Ember.isEmpty(user.get('company'))) {
        this.set('accountName', user.get('firstName') + ' ' + user.get('firstName'));
      } else {
        this.set('accountName', user.get('company.name'));
      }
    });
  })
});

My session-user service looks like the following:

//services/session-user.js
import Ember from 'ember';
import DS from 'ember-data';

const { service } = Ember.inject;

export default Ember.Service.extend({
  session: service('session'),
  store: service(),

  user: Ember.computed('session.data.authenticated.userId', function() {
    const userId = this.get('session.data.authenticated.userId');
    if (!Ember.isEmpty(userId)) {
      return DS.PromiseObject.create({
        promise: this.get('store').find('user', userId)
      });
    }
  })
});

Now, a user has a company, and a company has opportunities. I would like to retrieve the company opportunities, based on the currently logged in user. How do I do this? In my opportunities route I have tried the following:

//routes/opportunities/index.js
import Ember from 'ember';

export default Ember.Route.extend({
  sessionUser: Ember.inject.service('session-user'),
  model: function(){
    this.get('sessionUser.user').then((user)=>{
      let companySlug = user.get('company.slug');
      console.log(companySlug);
      return this.store.findRecord('company', companySlug);
    });
  }
});

When using {{model.opportunities}} in my template, it just stays blank and looks like the promise never resolves. However, I can see the data populating in the Ember Inspector, as well as the expected output of my console logs. Further, when I do the following, it works fine:

//routes/opportunities/index.js
import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
    let companySlug = 'known-company-slug';
    return this.store.findRecord('company', companySlug);
  }
});

Which means that the problem lies with model not resolving/updating for some reason. What am I doing wrong here?

Okay so I was trying to get the company model when I already had access to it through the sessionUser service.

//routes/opportunities/index.js
import Ember from 'ember';

export default Ember.Route.extend({
  sessionUser: Ember.inject.service('session-user'),
  model: function(){
      return this.get('sessionUser.user').then(user => user.get('company'));
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM