I'm currently using EmberSimpleAuth for authentication in my Ember CLI Application. My application has a shopping cart model that needs to be loaded after the user logs in. I've tried a couple approaches:
Approach One: Load it in the Application Route
//routes/application.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
Ember.run.later(function {
this.get('store').find('cart', this.get('session.cartId'));
});
},
setupController: function(controller, model) {
controller.set('cart', model);
}
});
This works if the app is in an authenticated state, or immediately transitions to one one boot up, but if the app fully loads in an unauthenticated state, then cartId is null and Ember throws and exception. Plus it won't reload the route when moving to an authenticated state.
Approach Two: Move it to the application controller
//controllers/application
import Ember from 'ember';
export default Ember.Controller.extend({
cart: Ember.computed('session.cartId', function() {
var self, cartId;
self = this;
if (!Ember.isEmpty(cartId)) {
return Ember.run(function() {
self.store.find('cart', cartId);
});
}
else {
Ember.Object.create({});
}
})
});
This seemed to get around the problem, however I don't like it at all. It seems wrong. What's the appropriate way to key off application-level queries based on information gained in an initializer?
Aucun commentaire:
Enregistrer un commentaire