【问题标题】:Access target route parameters in beforeModel在 beforeModel 中访问目标路由参数
【发布时间】:2015-07-06 23:44:22
【问题描述】:

在 Ember 中加载路由时,如何访问 beforeModel 函数中的目标路由参数?

文档中提到 beforeModel 是中止或重定向转换的正确位置 - 但如果没有目标参数,我无法添加所需的逻辑来确定我们是否应该重定向。

App.PostRoute = Ember.Route.extend({
  beforeModel: function(transition) {

    // transition doesn't seem to have the target params

  }
});

我也找不到任何关于 API 文档中描述的 Transition 对象的明确文档:

http://emberjs.com/api/classes/Ember.Route.html#method_beforeModel

【问题讨论】:

  • 应该是transition.params['routeName'].identifier
  • transition.to.params 应该有它们

标签: ember.js ember-router


【解决方案1】:

从路线上,你可以使用:

App.PostRoute = Ember.Route.extend({
  beforeModel: function(transition) {
    var params = this.paramsFor('post');
  }
});

但不要忘记在对应的控制器中声明参数:

App.PostController = Ember.Controller.extend({
  queryParams: 'author title'.w()
});

在这里,例如,我声明 author & title 允许的参数。其他任何内容都不会被解析也不会传递给paramsFor调用者。

【讨论】:

    猜你喜欢
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2020-01-01
    • 2018-12-22
    相关资源
    最近更新 更多