我想知道什么正确的方式来访问模型从控制器?什么是从Ember的控制器访问模型的正确方法
我注意到,在控制器的初始化的模式仍然是空
#controller.js
init(){
console.log(this.model); // IS NULL
}
但setupController方法有填充模型。因此,目前我正在从setupController调用控制器的方法,并在那里传递模型。这很好吗?
我想在控制器中会有一个回调方法,当控制器安装时会自动调用它。
我想知道什么正确的方式来访问模型从控制器?什么是从Ember的控制器访问模型的正确方法
我注意到,在控制器的初始化的模式仍然是空
#controller.js
init(){
console.log(this.model); // IS NULL
}
但setupController方法有填充模型。因此,目前我正在从setupController调用控制器的方法,并在那里传递模型。这很好吗?
我想在控制器中会有一个回调方法,当控制器安装时会自动调用它。
route.js
model() {
return this.store.findAll("post");
},
setupController(controller, model){
controller.set('model', model);
}
这会给控制台日志模式,即交对象的集合。
controller.js
init(){
console.log(this.model);
}
特别是如果你使用 你选择什么将是你的控制器上的模型RSVP承诺我们做到这一点大部分的时间。
例
model(params) {
return Ember.RSVP.hash({
lecture: this.store.findRecord('section', params.section_id).then((section)=>{
return this.store.createRecord('lecture',{
section: section
});
}),
section:this.store.findRecord('section', params.section_id),
course: this.store.query('course',{filter:{section_id:params.section_id}})
});
},
setupController(controller,model){
controller.set('model', model.lecture);
controller.set('section', model.section);
controller.set('course', model.course);
}
注意,如果你只对路线
model(params) {
return this.store.findRecord('course', params.course_id);
}
只是简单的模型,你不有做对控制器的任何设置这是可能的,这也将会给你的模型在控制器上。
嗯。我明白你的意思了。换句话说。我所做的并不是错的吗? –
如果你做了这件事情你没有错。 –
听起来很好,谢谢。 –
setupController
钩子方法将模型设置为控制器的属性。
setupController(controller,model){
this._super(...arguments);
}
您可以像控制器中的其他属性一样获取模型。 this.get('model')
这里回答你的问题。 http://stackoverflow.com/questions/27332840/how-to-access-ember-model-in-oninit-in-object-controller –