sendAction()在Ember.Component默认情况下气泡到控制器,这是预期的.但我有2,3个动作,我宁愿发送到使用该组件的相应视图.在模板中,我们设置了要使用的操作target=view.我们可以这样做吗?
更新:目前作为一种解决方法,我将视图对象发送到组件,该组件从那里调用view.send()发送操作.但我觉得这不正确.
好吧经过一番思考我相信我知道你的意思.如果您有一个组件并且您有一个操作,它将由组件本身处理.如果要在组件外发送操作,可以使用sendAction.
现在,为了保持组件的视图,因为组件基于视图,您可以做到this.get('parentView')获取父视图然后链send('nameOfAction')
因此它将this.get('parentView').send('nameOfAction')来自组件操作,然后它将触发嵌入组件的父视图上的操作.
因此,在您的组件中,您可以:
App.DemoCompComponent = Ember.Component.extend({
actions: {
internalTrigger: function() {
//just an alert to make sure it fired
alert('Internal action was caught on component');
//this should target parent view component is in
this.get('parentView').send('viewTriggerTest');
}
}
});
Run Code Online (Sandbox Code Playgroud)
现在假设您在索引模板中有组件,您可以:
模板将是:
<script type="text/x-handlebars" data-template-name="index">
<h2>Inside Index Template</h2>
{{demo-comp}}
</script>
Run Code Online (Sandbox Code Playgroud)
索引查看代码将是:
App.IndexView = Ember.View.extend({
actions: {
viewTriggerTest: function() {
alert('View Trigger Test Worked on Index!');
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个jsbin http://emberjs.jsbin.com/reyexuko/1/edit
| 归档时间: |
|
| 查看次数: |
2255 次 |
| 最近记录: |