我创建了一个呈现 html 文本并处理点击事件的组件。
Ember 组件不会冒泡事件,但我想让点击链接冒泡到窗口并让浏览器处理它。
代码讲述了超过 1000 个单词:-)
HTML
<script type="text/x-handlebars" data-template-name="application">
<h1>Click the link in the following div</h1>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
{{test-a}}
</script>
<script type="text/x-handlebars" data-template-name="components/test-a">
<div class="index" {{action 'edit' on="click"}}>
<a href="http://www.example.com" target="_blank">Click me</a>
</div>
</script>
咖啡
App = Ember.Application.create({});
App.TestAComponent = Ember.Component.extend
actions:
edit: ->
if event.toElement.nodeName.toUpperCase() == 'A'
return true # should bubble to the window but is a component
# do something for editing here
alert('div clicked')
false
CSS
h1 { font-size: 1.6em; padding-bottom: 10px; }
h2 { font-size: 1.4em; }
ul { padding: 15px; font-size: 1.4em; color: green; }
.index { background-color: #666; padding: 20px; }
fiddle :http://jsfiddle.net/ujrZL/
最佳答案
当您创建组件时,设置应使用 sendAction 传播的操作的操作名称。
{{test-a internalAction='myAction'}}
{{test-a internalAction='myAction2'}}
从组件内部
this.sendAction('internalAction');
关于ember.js - 从 ember 组件冒泡事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776520/