Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember : handling multiple events with {{action}} tag?

I'm using Ember.js and I'm trying to have a div element with two different actions : one when mouse enter and one when mouse leave. I tried to do :

<div {{action "stopInfo" on="mouseLeave"}} {{action "startInfo" on="mouseEnter"}}>

But it only trigger the first action (mouseleave).

Is there a way to have 2 actions on the same element ? Thanks

like image 774
Stéphane Piette Avatar asked Jul 18 '12 15:07

Stéphane Piette


1 Answers

As per issue #569 multiple action helpers for a tag is not supported. To handle multiple events you should use a custom Ember.View for that. In your case something like this, see http://jsfiddle.net/pangratz666/2V9cP/:

Handlebars:

{{#view App.ActionView}}
    ... content of div ...
{{/view}}

JavaScript:

App.ActionView = Ember.View.extend({
    stopInfo: function(evt) { console.log('stop info'); },
    startInfo: function(evt) { console.log('start info'); },

    click: Ember.alias('stopInfo'),
    mouseLeave: Ember.aliasMethod('stopInfo'),

    mouseEnter: Ember.aliasMethod('startInfo')
});​

I've used the Ember.alias helper here, just to illustrate how I would use the same function for multiple view events ...

like image 141
pangratz Avatar answered Sep 22 '22 15:09

pangratz