【发布时间】:2013-11-15 14:11:00
【问题描述】:
我创建了一个渲染 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; }
【问题讨论】:
-
使用 {{action}} 将无法正常工作。因为它将执行 evt.preventDefault()。您当前的代码是一个测试,您能分享您的意图吗?所以我可以考虑一些解决方案。谢谢。
标签: ember.js