问题
I'm building a Twitter based app in Ember-CLI and have a session initializer to ensure users are authenticated on each route. Users login via Twitter using the passport-twitter module for Express.js. I've done this before in a previous app and it's fine, but in this app I've noticed that every route reloads the page.
I've stripped everything out to just an h1 tag per route and it still reloads the page. As soon as I remove the initializer and beforeModel hook below however, everything works as expected with no page reload.
initializers/session.js :
name: 'session',
initialize: function(container, app) {
var Session = Ember.Object.extend({
user: null,
isAuthenticated: function() {
return this.get('user') !=null;
}.property('user')
});
app.register('session:main', Session);
app.inject('route', 'session', 'session:main');
app.inject('controller', 'session', 'session:main');
}
routes/application.js
beforeModel: function() {
var route = this;
var promise = this.store.find('user', {operation: 'authenticated'});
return promise.then(function(users) {
if (users && users.get('length') > 0) {
var user = users.get('firstObject');
route.set('session.user', user);
}
return users;
});
},
I've tried setting {singleton: true} in app.register just in case, but that doesn't work either.
I'm using a more recent version of Ember 1.11.1 and Ember-CLI 0.2.3 and I'm not sure how to even debug this. Any help much appreciated.
回答1:
A bit of a guess, since I cannot see what exactly do you mean by "reloading the page". I'm going to assume it's "nothing rendered/blank page" phase.
From Ember's beforeModel documentation:
if the value returned from this hook is a promise, the transition will pause until the transition resolves. Otherwise, non-promise return values are not utilized in any way.
beforeModel
is first hook on entry, a place, where one can decide about driving user off the route, so nothing will render until given promise/transition is resolved. If your application is setup in a way hook is run repeatedly it might result in "refreshing-like" experience.
You could either wait for transition to finish transition.then(..your setup...)
, not return a promise, or set session.user
value to a promise.
回答2:
I think you were on the right trail with { singleton: false }
, but I believe that has changed in the last few versions of Ember. According to the guide, you need to use { instantiate: false }
to tell the Ember container that you're giving it an already instantiated object. Also, be sure to use Ember.Object.create()
instead of Ember.Object.extend()
. That will ensure that you get the same session instance every time.
(This actually assumes that you have code somewhere that redirects/refreshes if the session isn't active, but I'm assuming that is the case because if not, why would you even have the session?)
来源:https://stackoverflow.com/questions/32312552/ember-session-initializer-causing-page-reload-for-every-route