jeudi 2 juin 2016

Ember-data models that load in application.js not loaded after ember-simple-auth authentication

I am currently loading some ember-data which is used site-wide from the routes/application.js route. That data can then be accessed anywhere. In particular, the data is passed to a component from the templates/application.hbs template, and it's store.peekAll() accessed from a variety of other places.

Whether this is the right approach or not (and I welcome feedback on that!), this works for me, EXCEPT in the following case:

If the user is not authenticated, I can't query for the data, because they are not yet authorized to view it. This is the code I have:

// routes/application.js
import Ember from 'ember';

export default Ember.Route.extend(ApplicationRouteMixin, {
  model() {
    if (this.get('session.isAuthenticated')) {
      return Ember.RSVP.hash({
        clients: this.store.findAll('client', {include: 'projects'}),
        resources: this.store.findAll('resource')
      });
    }
  }
});

I would like to make the data load after they authenticate, but I don't know how to make the model re-load. If I just have the data load in the sessionAuthenticated hook, as follows:

// routes/application.js
sessionAuthenticated() {
  this.controller.set('clients', this.store.findAll('client', {include: 'projects'}));
  this._super(...arguments);
}

It doesn't work. The store gets filled with the data, but the components that depend on this data never see it. Also, the route that I transition to next, which also depends on the data, doesn't have it in time because of synchronicity.

There must be a simple way to do this, but I'm baffled as to what it is. Observers? Forcing the routes/application.js model() method to re-run (and wait until the promise returns) now that session.isAuthenticated is true?

ember: 2.5.x, ember-data: 2.5.x




Aucun commentaire:

Enregistrer un commentaire