每当 ember 应用程序发生 url 变化时触发 route/controller 挂钩

Fire route/controller hooks every time url changes in ember app

我在这个应用程序中有两条路线,它们由在 rails 端具有访问控制逻辑的模型支持。因此,当他们第一次加载到应用程序时,他们有一个 isUnlocked 属性,我会在模型加载后检查它。如果 属性 没有解锁,路由应该重定向。

所以如果我的路由器是这样的:

this.route('thing', { path: 'thing/:thing_id' }, function() {
  this.route('resource', { path: 'resource/:resource_id' });
});

我的 "resource" 路线延伸如下:

import AuthenticatedRoute from 'doki/routes/authenticated';

export default AuthenticatedRoute.extend({

  requireDean: false,

  activate() {
    this._super();
    this.checkAccess();
  },

  afterModel() {
    this._super();
    this.checkAccess();
  },

  resetController() {
    this._super();
    this.checkAccess();
  },

  checkAccess() {
    // here is where I'll check the model's isUnlocked property and 
    // redirect if it's false or not set
    console.log('checkAccess');
  }

});

当我输入 /thing/1/resource/1 时,resource=1 的模型由 ThingResourceRoute 加载,但是如果 resource=2 已经加载到商店中,如果我单击 /thing/1/resource/2,activate 不触发,setupController 不触发,等等,所以我不确定在哪里进行 checkAccess() 测试。

每当 URL 更改时检查 isUnlocked 属性 的最佳位置是什么,因为 "activate"、"resetController" 等,当 URL 更改为相同路线但具有不同 isUnlocked 的不同项目 属性.

有没有我可以实现的钩子,它总是会被调用?在 renderTemplate 中进行访问检查似乎可行,但那似乎不是正确的地方。

我是否应该在通过 API 调用更新模型后使模型无效?如果我在本地将 isUnlocked 设置为 true(并且不通过 API 持久化模型),我将在 route/controller 链中的哪里添加检查,每次尝试时它都会检查"access"那个型号?

看起来 didTransition 事件在每次输入 URL 时都会触发——即使模型已经加载,控制器已经设置,挂钩都已触发——所以我将检查 didTransition 中的模型:

actions: {
    didTransition: function() {
      console.log('-- transitioned into', this.routeName, this.get('controller.model'));
    }
}