and*_*zko 11 javascript events components event-bubbling ember.js
我创建了一组嵌套组件.
代码在这里:http://emberjs.jsbin.com/hasehija/2/edit.
HTML:
{{#level-1}}
{{#level-2}}
{{#level-3}}
<button {{action 'handleAction'}}>
Click me (yielded)
</button>
{{/level-3}}
{{/level-2}}
{{/level-1}}
Run Code Online (Sandbox Code Playgroud)
JS:
App.ApplicationController = Ember.Controller.extend({
actions: {
handleAction: function() {
alert('Handled in ApplicationController');
}
}
});
App.Level1Component = Ember.Component.extend({
actions: {
handleAction: function() {
alert('Handled in Level 1');
}
}
});
App.Level2Component = Ember.Component.extend({
actions: {
handleAction: function() {
alert('Handled in Level 2');
}
}
});
App.Level3Component = Ember.Component.extend({
actions: {
handleAction: function() {
alert('Handled in Level 3');
this.set('action', 'handleAction');
this.sendAction();
}
}
});
Run Code Online (Sandbox Code Playgroud)
我想要实现的是以下列方式鼓泡事件:
Level3Component -> Level2Component -> Level1Component -> ApplicationController
不幸的是,我无法处理任何组件内的事件; 事件冒出来了ApplicationController.
有没有办法强制组件的操作在组件的整个层次结构上冒泡,以便我有4次显示警报(当然在添加this.sendAction之后)?
再一次,这是您可以使用的代码:http://emberjs.jsbin.com/hasehija/2/edit.
ppc*_*ano 19
根据您的示例,您必须将组件targetObject属性定义为:
App.Level3Component = Ember.Component.extend({
action: 'handleAction',
targetObject: Em.computed.alias('parentView'),
actions: {
handleAction: function() {
alert('Handled in Level 3');
this.sendAction();
}
}
});
Run Code Online (Sandbox Code Playgroud)
http://emberjs.jsbin.com/hasehija/5/edit