Ember + Ember数据路径错误处理问题

时间:2014-03-29 16:53:47

标签: ember.js routes ember-data

在我的应用程序中的soume路径中,错误操作永远不会被触发,我无法弄清楚原因。在某些路由上,错误操作正常。

这是申请途径:

Simitu.ApplicationRoute = Ember.Route.extend({
  init: function() {
    this._super();
    Simitu.AuthManager = Simitu.AuthManager.create();
  },
  model: function() {
    if (Simitu.AuthManager.get('session.user'))
      return this.store.find('admin', Simitu.AuthManager.get('session.user'));
  },
  actions: {
    error: function(reason, transition) {
      if (reason.status === 401) {
        Simitu.AuthManager.reset();
        this.transitionTo('login');
      }
    }
  }
});

在此路线上错误从未触发

Simitu.PlacesIndexRoute = Ember.Route.extend({
  model: function() {
    var self = this;

    // force adapter request
    this.store.find('place');
    return this.store.filter('place', function(record) {

      // return just places that belongs to this client / application
      return record.get('client_id') === self.modelFor('client');
    });
  },
  actions: {
    createNew: function() {
      var place = this.store.createRecord('place');
          // tree structure in places is not implemented yet
          //parent = this.store.find('place', params.place_id);

      place.set('client_id', this.modelFor('client'));

      // open place
      this.transitionTo('place', place);
    },
    error: function(error, transition) {
      return true;
    }
  }
});

在这条路线上一切正常:

Simitu.ClientsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('client');
  },
  actions: {
    error: function() {
      return true;
    }
  }
});

有人想要为什么?

3 个答案:

答案 0 :(得分:1)

error操作会在资源上触发,而不是单个路径。

http://emberjs.jsbin.com/cayidiwa/1/edit

答案 1 :(得分:0)

这就是我的路由器的样子。由于模型中的嵌套或过滤逻辑,它可能会中断。我在路线中的beforeModel钩子中修复了它,但仍然不知道我的第一个解决方案有什么问题。

Simitu.Router.map(function () {
  this.resource('login');
  this.resource('clients');
  this.resource('client', { path: 'clients/:client_id'}, function() {
    this.resource('places', function() {
      this.resource('place', { path: ':place_id' });
    });
    this.resource('placecategories',{ path: 'places-categories' }, function() {
      this.route('new');
    });
  });
});

答案 2 :(得分:0)

我将一些auth处理逻辑移到 beforeModel hook。

Simitu.AuthRoute = Ember.Route.extend({
  beforeModel: function(transition) {
    if (!Simitu.AuthManager.isAutenticated()) {
      this.redirectToLogin(transition);
    }
  },
  redirectToLogin: function(transition) {
    this.transitionTo('login');
  },
  actions: {
    error: function(reason, transition) {
      if (reason.status === 401) {
        Simitu.AuthManager.reset();
        this.redirectToLogin(transoition);
      }
    }
  }
});