简体   繁体   中英

Bubble up events from ember component

I've created a component which renders html text and handles click events.

Ember Components are not bubbling up events but I would like to let clicks on links bubble up to the window and let the browser handle it.

Code tells more than 1000 words :-)

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>

Coffee

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/

When you create a component set the action name of the action that should be propogated using sendAction.

{{test-a internalAction='myAction'}}

{{test-a internalAction='myAction2'}}

And from inside the component

 this.sendAction('internalAction'); 

http://emberjs.jsbin.com/oPAtORO/2/edit

I'm using a workaround which might be the only way currently. I don't like it because it smells a bit for me.

CoffeeScript

actions: ->
    edit: ->
        window.open event.toElement.href, '_blank'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM