Chr*_*ini 4 ember.js ember-cli
我在我的model钩子里使用RSVP.hash .但是我需要我的路线来加载基于url(包含动态段)的动态数据.即this.route('foo', {path: ':id'}).
所以我决定将一些东西移出afterModel钩子.
但是,我需要执行商店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);
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有这个,它有效:
model(params) {
this.set('params', params);
...
},
afterModel: function(model) {
console.log(this.get('params')); // logs the right params
...
}
Run Code Online (Sandbox Code Playgroud)
但是,这是访问的唯一途径params在afterModel钩?
这种方法是否合理?
所述afterModel钩子提供了一个名为第二个参数transition.你可以使用这样的路径从它获取参数:transition.params.{route-name}.{param-name}所以考虑你的例子:
//let's say this is BazRoute and BazController:
model(params) {
return this.store.findRecord('foo', params.foo_id);
},
afterModel: function(model, transition) {
const params = transition.params.baz;
console.log(params); // logs the right 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);
});
}
Run Code Online (Sandbox Code Playgroud)