我在我的 model
中使用了 RSVP.hash钩。但是我需要我的路由来加载基于 url 的动态数据(其中包含一个动态段)。即this.route('foo', {path: ':id'})
.
所以我决定把一些东西移出,到afterModel
。取而代之的是钩子(Hook)。
但是,我需要使用 params
执行存储。 (用于分页):
model(params) {
this.set('params', params);
return this.store.findRecord('foo', params.foo_id);
},
afterModel: function(model) {
console.log(this.get('params')); // logs the right params
let params = this.get('params');
// This store query needs access to params
this.store.query('bar', { filter: { 'foo-id': model.get('id') }, page: { number: (params.page ? params.page : 1) } }).then(bars => {
this.controller.set('bars', bars);
});
}
setupController(controller, model) {
this._super(controller, model);
this.set('bars', bars);
}
到目前为止,我有这个,它有效:
model(params) {
this.set('params', params);
...
},
afterModel: function(model) {
console.log(this.get('params')); // logs the right params
...
}
但这是访问
params
的唯一方法吗?在 afterModel
钩?这种方法合理吗?
最佳答案
使用 this.paramsFor(this.routeName)
函数来获取一个带有参数的普通对象。
关于ember.js - 如何在 afterModel Hook 中访问参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34934053/