get action target element in ember

为君一笑 提交于 2019-12-31 01:44:32

问题


How can I get the target element's object on which a click event was fired. For eg, if I have a template like:

<table>
<tr>
    <th {{action "someAction"}} >Type</th>
</tr>
</table>

and in my controller I have corresponding action defined like:

action:{
    someAction : function(){
        //get the th element object 
    }
}

It seems like event object is not passed, because I had tried

action:{
    someAction : function(event){
        //event is undefined!
    }
}

UPDATE : Thanks Márcio and Jeremy. I've now got a working solution. My template looks like:

<thead>
        {{#view AS.MyView sortBy="value"}}Value{{/view}}
</thead>

My view looks like:

AS.MyView = Ember.View.extend({
attributeBindings: ['sortBy'],
classNames: ['sortable'],
tagName: 'th',
click: function(evt) {
    var target = evt.target, self = this;
    this.get('controller').send('setSort',$(target).attr('sortBy'));

    $(target).parent().find('th').removeClass('desc').removeClass('asc');
    $(target).addClass(self.get('controller.sortAscending')?'asc':'desc');
}
});

回答1:


classNameBinding is a great option for this use case.

Just for reference, if you need to deal with low level browser events, you can do that in the View layer.

http://emberjs.com/guides/views/handling-events/

{{#view App.ClickableView}}
This is a clickable area!
{{/view}}

App.ClickableView = Ember.View.extend({
  click: function(evt) {
    alert("ClickableView was clicked!");
  }
});


来源:https://stackoverflow.com/questions/18833951/get-action-target-element-in-ember

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!