【问题标题】:Trigger action whenever session is updated, on application restart每当会话更新时触发操作,在应用程序重新启动时
【发布时间】:2016-04-17 08:30:34
【问题描述】:

我正在使用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。如何收听“会话初始化”事件?

【问题讨论】:

    标签: ember.js ember-simple-auth http-token-authentication


    【解决方案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'),
    

    【讨论】:

    • 谢谢,很有趣。在哪里可以找到有关 .on() 运算符的文档?
    • 而且那个语法似乎很旧,是吗?我在 Ember 2.2.0 上。
    • @gonvaled 是的,我使用的是 Ember 1.13。新语法应该是 => loggedIn: Ember.observer('session.isAut...', function() {
    猜你喜欢
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多