我有一个患者对象的模型
App.Router.map (function () {
this.resource('patients');
this.resource('patient', {path: ':patient_id'}, function(){
this.resource('dashboard', function() {
this.route('summary');
});
});
});
App.PatientRoute = Ember.Route.extend({
model: function(params) {
return App.Patient.find(params.patient_id);
},
setupController: function(){
console.log("Menu Items:" + App.PatientMenuItem.find() );
this.controllerFor('patient').set('menuItems', App.PatientMenuItem.find());
}
});
App.DashboardSummaryRoute = Ember.Route.extend({
setupController: function(){
this.controllerFor('dashboard.summary').set('content', this.controllerFor('patient').get('model'));
this.controllerFor('dashboard.summary').getPatient();
}
});
App.DashboardSummaryController = Ember.ObjectController.extend({
getPatient:function(){
console.log(this.content.get_allergies);
}
});
App.PatientController = Ember.ObjectController.extend({
menuItems:[],
});
<script type="text/x-handlebars" data-template-name="dashboard/summary">
Summary{{this.content.get_allergies}}
</script>
在上面,我无法在 DashboardSummaryController 中访问相同的 get_allergies,但我可以在 Handlebars 中访问它,任何人都可以帮助我,这是什么错误?
提前致谢
最佳答案
我不知道单独这样做是否可以解决问题,但是在访问属性时始终使用 get() 和 set() 方法。所以我建议在 getPatient() 方法中尝试一下:
App.DashboardSummaryController = Ember.ObjectController.extend({
getPatient:function(){
console.log(this.get("content.get_allergies"));
}
});
为什么模板有效?您那里的 Handlebars 表达式会自动转换为调用,我已建议您的 Controller 方法。
关于ember.js - 为什么模型在 Controller 内部无法访问,而在 Handlebars 模板中可以访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15199600/