首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember数据路由错误处理问题

Ember数据路由错误处理问题
EN

Stack Overflow用户
提问于 2014-03-29 16:53:46
回答 3查看 761关注 0票数 0

在我的应用程序中,错误操作从来没有触发过,我也不知道为什么。在某些路由上,错误操作可以正常工作。

这是申请路线:

代码语言:javascript
复制
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');
      }
    }
  }
});

在此路由上的错误是从未触发

代码语言:javascript
复制
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;
    }
  }
});

在这条路上,一切都很好:

代码语言:javascript
复制
Simitu.ClientsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('client');
  },
  actions: {
    error: function() {
      return true;
    }
  }
});

有人知道为什么吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-30 15:49:17

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

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

票数 1
EN

Stack Overflow用户

发布于 2014-04-01 08:46:09

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

代码语言:javascript
复制
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');
    });
  });
});
票数 0
EN

Stack Overflow用户

发布于 2014-04-10 08:03:59

我将一些处理逻辑转移到beforeModel钩子。

代码语言:javascript
复制
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);
      }
    }
  }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22733926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档