Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ember.js(应用程序控制器和路由)中的事件顺序_Ruby On Rails_Ember.js - Fatal编程技术网

Ruby on rails Ember.js(应用程序控制器和路由)中的事件顺序

Ruby on rails Ember.js(应用程序控制器和路由)中的事件顺序,ruby-on-rails,ember.js,Ruby On Rails,Ember.js,我正在从ApplicationController init事件中的元标记获取当前用户: App.ApplicationController = Ember.Controller.extend({ needs: ['currentUser'], init: function() { 'use strict'; this._super(); var attributes = $('meta[name="current-user"]').attr('conten

我正在从ApplicationController init事件中的元标记获取当前用户:

App.ApplicationController = Ember.Controller.extend({
  needs: ['currentUser'],

  init: function() {
    'use strict';

    this._super();

    var attributes = $('meta[name="current-user"]').attr('content');

    if (attributes) {
      this.set('controllers.currentUser.content', App.User.create().setProperties(JSON.parse(attributes)));
    }
  },
在每个需要对用户进行身份验证的路由中,我都有一个重定向钩子,如果未设置currentUser,该钩子应该重定向:

App.UserEditRoute = Ember.Route.extend({
  redirect: function() {
    if (this.controllerFor('currentUser').get('isSignedIn') === false) {
      this.transitionTo('user.login');
    }
  }
});
问题是路由重定向事件在ApplicationController.init之前触发

什么是正确的方式让这一切继续下去


谢谢

而不是
ApplicationController.init()
此逻辑属于路由。像这样的方法应该会奏效:

App.ApplicationRoute = Ember.Route.extend({
  beforeModel: function() {
    var attributes = $('meta[name="current-user"]').attr('content');
    if (attributes) {
      var user = App.User.create().setProperties(JSON.parse(attributes));
      this.controllerFor('currentUser').set('content', user);
    }
  }
});

谢谢你,迈克。有没有最好的方法可以避免在每个路由中都进行编码?应该只在App.ApplicationRoute中需要这样做