使用classNameBindings并从子视图发送事件来更新属性。
考虑这个例子:
{{#view App.AParentView}}
{{view App.AChildView}}
{{/view}}
的应用:
App = Ember.Application.create();
App.AParentView = Em.View.extend({
bgColor: 'lightgray',
classNames: ['parent-view'],
classNameBindings: 'bgColor',
updateBg: function(bg) {
this.set('bgColor', bg);
}
});
App.AChildView = Em.View.extend({
classNames: ['child-view', 'blue'],
willInsertElement: function() {
this.get('parentView').send('updateBg', 'green');
}
});
的CSS,看看它实际工作:
html, body {
margin: 20px;
}
.parent-view {
padding: 4rem;
}
.child-view {
padding: 2rem;
}
.lightgray {
background-color: lightgray;
color: black;
}
.blue {
background-color: blue;
}
.green {
background-color: green;
}
见jsbin here。