我正在尝试使用此Ember指南中描述的'needs'语法在父ArrayController和子ObjectController之间建立关系 - http://emberjs.com/guides/controllers/dependencies-between-controllers/
当我尝试访问控制器对象以获取对子进程的父对象的引用时,我得到'controllers object undefined'错误。任何帮助赞赏!
Ember版本RC4
模板:
<script type="text/x-handlebars" data-template-name="gigs">
<div> // code simplified
{{#each controller itemController="gig"}}
{{#view App.GigView contentBinding="this"}}
<div class="tile">
<img {{bindAttr src="photo_url"}} />
{{#if widgetDisplayed}}
// widget view
{{/if}}
</div>
{{/view}}
{{/each}}
</div>
</script>
使用Javascript:
App.GigsController = Ember.ArrayController.extend({
anyWidgetDisplayed: false,
isAnyWidgetDisplayed: function() {
return anyWidgetDisplayed;
}
});
App.GigController = Ember.ObjectController.extend({
needs: ["gigs"],
widgetDisplayed: false,
displayWidget: function() {
console.log(controllers.gigs);
if (!controllers.gigs.isAnyWidgetDisplayed) {
this.set("widgetDisplayed", true);
}
}
});
答案 0 :(得分:2)
当您根据需要使用控制器时,您应该使用get函数
来获取控制器displayWidget: function() {
var gigsController = this.get('controllers.gigs')
console.log(gigsController);
if (!gigsController.get('isAnyWidgetDisplayed')) {
this.set("widgetDisplayed", true);
}
}
或者当你使用itemController时,你不需要使用需要获取parentController,你可以使用parentController属性
App.GigController = Ember.ObjectController.extend({
widgetDisplayed: false,
displayWidget: function() {
var gigsController = this.get('parentController');
console.log(gigsController);
if (!gigsController.get('isAnyWidgetDisplayed')) {
this.set("widgetDisplayed", true);
}
}
});