似乎在嵌套路由的情况下,每个路由的model
钩子在调用任何setupController
钩子之前被调用。那么子路由如何安全地访问父模型呢?
就我而言,模型实际上并不是持久实体。它们都是基于参数在模型挂钩中生成的。所以我不认为Parent.find()
/ ajax / promise 似乎是要走的路。理想情况下,父模型应该可以通过 访问ParentController
,不是吗?还是这种方法不符合最佳实践?
App.Router.map(function() {
this.resource("parent", { path: "parent/:name" }, function() {
this.route('child');
});
});
App.ParentRoute = Ember.Route.extend({
model: function(params) {
// build a model here, based on params.
// no asynch stuff happening, should be good to go.
return { lastName: params.name }
},
setupController(controller, model) {
controller.set('model', model);
}
});
App.ChildRoute = Ember.Route.extend({
model: function(params) {
parentModel = this.controllerFor('parent').get('model');
return {lastName: parentModel.get('name') + 'son' };
// parent Model is null
// need the parent model HERE,
// but I can't get it through the controller yet
// as setupController in the parent route hasn't been called yet
}
});