0

我在 ApplicationRoute 中使用了经典的 Ember-simple-auth 设置

model: function () {
  return Ember.RSVP.hash({
    user: this.store.find('gsUser').then(function(data) {
      return data.get('content')[0]
    })
  });
},

setupController: function(controller, model) {
  this.controllerFor('user').set('content', model.user);
}

当用户失去授权时,您打开页面。ApplicationRoute::model 首先被触发,服务器返回 401 并停止其他执行。

GET http://localhost:8000/app_dev.php/api/1/users.json 401 (Unauthorized)
Error while loading route: undefined 

model只有在身份验证成功时才应触发。

我看到有,sessionAuthenticationSucceeded但我已经尝试了所有的方法来听它,没有人工作。用户通过身份验证后如何监听此事件并从服务器获取数据?

11/06 22:57 更新:enter code here

我已经设法解决了这个问题的一个解决方案,但它似乎完全不是余烬方式

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, {
  skipModelLoading: false,

  beforeModel: function() {
    this.set('skipModelLoading', !this.get('session').get('isAuthenticated'));
  },

  model: function () {
    if (this.get('skipModelLoading')) {
      return;
    }

    return Ember.RSVP.hash({
      user: this.store.find('gsUser').then(function(data) {
        return data.get('content')[0]
      })
    });
  },

  setupController: function(controller, model) {
    if (this.get('skipModelLoading')) {
      return;
    }

    this.controllerFor('user').set('content', model.user);
  }
});
4

2 回答 2

1

我假设您正在使用该model方法加载经过身份验证的用户。我会采取不同的做法并将该属性附加到会话中,如下例所示:https ://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html#L101

于 2014-06-11T20:11:20.673 回答
1

我想我为我的问题找到了一个更有效的解决方案:

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, {
  onSessionIsAuthenticated: function () {
    var isAuthenticated = this.get('session').get('isAuthenticated');

    if (!isAuthenticated) {
      return false;
    }

    var userController = this.controllerFor('user');

    return Ember.RSVP.hash({
      user: this.store.find('gsUser').then(function (data) {
        userController.set('content', data.get('content')[0]);
      })
    });
  }.observes('session.isAuthenticated').on('init')
});
于 2014-06-13T20:56:46.503 回答