我目前有一个视图设置,根据模型呈现模板:
<ul>
{{#each controller.sortedAll}}
{{view App.ScoreView}}
{{/each}}
</ul>
App.ScoreView = Ember.View.extend({
templateName: function(){
var item = this.get('context')
if (item.sort < 8){
return 'low'
} else {
return 'high'
}
}.property(),
})
我正在努力为每个单独的视图分配特定的控制器。 jsbin是:http://jsbin.com/tahag/5/edit
是否可以使用App.ScoreView
在controller:
中指定控制器?或者我会更好地尝试设置项目控制器:
{{view App.ScoreView itemController="VAL"}}
从父控制器传递VAL
作为属性?
答案 0 :(得分:3)
控制器继承自当前范围(在视图上定义itemController不会做任何事情)。你应该在每一个上做。
{{#each controller.sortedAll itemController='val'}}
{{view App.ScoreView}}
{{/each}}
然后在视图中,您可以this.get('controller')...
如果你想在每个项目上使用不同类型的控制器,你也可以执行if语句并执行{{render 'high' this}}
。
{{#each controller.sortedAll}}
{{#if isBlue}}
{{render 'blue' this}}
{{/if}}
{{#if isGreen}}
{{render 'green' this}}
{{/if}}
{{/each}}
为了避免让你的模板超级复杂,我只会使用一个控制器。
{{#each controller.sortedAll itemController='score'}}
{{input value=sort}}
{{view App.ScoreView}}
{{/each}}
添加计算属性所依赖的内容,以便自动更新:
App.ScoreView = Ember.View.extend({
templateName: function(){
var sort = this.get('controller.sort');
if (sort < 8){
return 'low';
} else {
return 'high';
}
}.property('controller.sort'),
});
利用computed
助手
App.ScoreController = Ember.ObjectController.extend({
isVeryHigh: Em.computed.gt('sort', 20),
isVeryLow: Em.computed.lt('sort', 4)
});