Sending an action to a view in Ember.js

action, ember.js, emblem.js, view

Solution

The syntax for sending events to views is, `{{action myEvent target="view"}}`. Then a `myEvent` handler in the corresponding view can handle this event. Without the target the event will go to the controller in that view's context.

I suspect what is happening is that `Emblem` is putting the `target` attribute on the element itself rather than into the action handler. Since `target` is a valid attribute as well. You can confirm this by looking at the generated html in the Elements tab of dev tools.

Unfortunately I don't have a workaround with Emblem. But this stuff definitely works with Handlebars templates.

Problem

I have a view which contains a close button: ``` .flash-message div class="close-button" click="view.removeFlash" = view view.content.thisView ``` The view itself reads: ``` Whistlr.AlertView = Ember.View.extend templateName: "_alert" removeFlash: -> alert "Close!" ``` However, when I click on the "close-button" div, nothing happens. I've tried rewriting the button a few different ways: ``` click="view.removeFlash" click="removeFlash" click="removeFlash" target="view" ``` I've also tried placing the action directly in the controller (though I'm not even sure there is a controller for the view): ``` Whistlr.AlertController = Ember.ObjectController.extend removeFlash: -> alert "I work!" ``` None of these approaches work. Perhaps it's not even possible to send an action to the view like I would with a controller? If not, how else can I approach this problem?

Original source