简体   繁体   English

ember.js + ember-simple-auth认证后如何加载应用程序模型?

[英]ember.js + ember-simple-auth how can I load application models after authentication?

I'm adding ember-simple-auth to handle authentication for an application I'm building. 我要添加ember-simple-auth来处理我正在构建的应用程序的身份验证。 Currently in the ApplicationRoute I'm using a model to load sidebar content. 当前在ApplicationRoute中,我正在使用一种模型来加载侧栏内容。

Some of the data is dependant on a user URL property which is returned with the auth token. 某些数据取决于与auth令牌一起返回的用户URL属性。

I'm refactoring my code to handle loading data for the authenticated user but I'm unsure where to put the model call to load the sidebar data. 我正在重构代码以处理通过身份验证的用户的数据加载,但是不确定将模型调用加载到何处以加载侧边栏数据。

I'm thinking it'd make sense to add an observer on the isAuthenticated property to trigger the model load or take my current routes and wrap them in a resource which is responsible for loading the model? 我认为在isAuthenticated属性上添加观察者以触发模型加载或采用我当前的路线并将它们包装在负责加载模型的资源中是否有意义?

Application Route 申请途径

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin,
{
    model: function()
    {
        return Ember.RSVP.hash(
        {
            collections: Ember.$.getJSON(this.session.get('user.url') + '/collection'),

            libraries: Ember.$.getJSON(ENV.api + '/library')
        });
    },


    setupController: function(controller, model)
    {
        controller.set('libraries', model.libraries);

        controller.set('collections', model.collections);
    }
});

Route Mapping 路线图

App.Router.map(function()
{
    this.route('login');

    // Authenticated Routes

    this.route('my-account');

    this.route('collection', { path: '/collection/:id' });

    this.route('item.new', { path: '/item/new' });

    this.route('item.edit', { path: '/item/:id' });

    this.route('library', { path: '/:slug' });
});

The ApplicationRouteMixin defines the sessionAuthenticationSucceeded action that is invoked whenever the session state changes from not authenticated to authenticated. ApplicationRouteMixin定义了sessionAuthenticationSucceeded操作 ,该操作在会话状态从未认证变为已认证时被调用。 You can also listen to the session's sessionAuthenticationSucceeded event 您还可以侦听会话的sessionAuthenticationSucceeded事件

您可以在路由中使用this.get('session.user_email')来获取经过身份验证的用户的电子邮件,然后使用该电子邮件从服务器中获取用户数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM