0

我正在使用ember-simple-auth(和ember-simple-auth-token,但我认为这不相关)。每当登录用户重新加载页面时,都会正确地重新创建会话。不过,我有一个问题:当用户输入凭据时,我正在运行以下代码:

// controllers/login.js

import Ember from 'ember';

import helpers from '../lib/helpers';

function refreshActiveUser(store, session) {
    store.find('user', 'me').then(function(user) {
        Ember.set(session, 'activeUser', user);
    });
}

export default Ember.Controller.extend({
    session: Ember.inject.service('session'),
    actions: {
        authenticate: function() {
            var identification = this.get('model.identification'),
                password = this.get('model.password'),
                authenticator = 'authenticator:token',
                store = this.get('store'),
                session = this.get('session');
            session.authenticate(authenticator, {identification, password}).then(function() {
                refreshActiveUser(store, session);
            });
        }
    },
});

但我不知道如何触发我refreshActiveUser的应用程序重新加载。如何收听“会话初始化”事件?

4

1 回答 1

1

如果我正确理解您的问题,那么您可以为session.isAuthenticated做一个观察者

loggedIn: function() {
        if(!this.get('session.isAuthenticated'))
            return;

        var store = this.get('store');
        var profileLoaded = get_personal_settings(store);
        var self = this;

        profileLoaded.then(function(profile) {
            // TODO: only used for user, save user, not the whole profile

            self.set('session', profile);
        });


}.observes('session.isAuthenticated').on('init'),
于 2016-01-12T17:06:37.690 回答