[英]ember.js + ember-simple-auth how can I load application models after authentication?
我要添加ember-simple-auth來處理我正在構建的應用程序的身份驗證。 當前在ApplicationRoute中,我正在使用一種模型來加載側欄內容。
某些數據取決於與auth令牌一起返回的用戶URL屬性。
我正在重構代碼以處理通過身份驗證的用戶的數據加載,但是不確定將模型調用加載到何處以加載側邊欄數據。
我認為在isAuthenticated屬性上添加觀察者以觸發模型加載或采用我當前的路線並將它們包裝在負責加載模型的資源中是否有意義?
申請途徑
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);
}
});
路線圖
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' });
});
ApplicationRouteMixin
定義了sessionAuthenticationSucceeded
操作 ,該操作在會話狀態從未認證變為已認證時被調用。 您還可以偵聽會話的sessionAuthenticationSucceeded
事件
您可以在路由中使用this.get('session.user_email')
來獲取經過身份驗證的用戶的電子郵件,然后使用該電子郵件從服務器中獲取用戶數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.