简体   繁体   English

Ember使用computed属性作为动作参数

[英]Ember using computed property as an action parameter

I'm building an app to be used on iOS and desktop. 我正在构建一个在iOS和桌面上使用的应用程序。 I need to add the on="eventType" parameter to some actions, and use mouseUp for desktop and touchEnd for mobile. 我需要在某些操作中添加on =“eventType”参数,并将mouseUp用于桌面,将touchEnd用于移动设备。 I can't seem to add both like on="eventOne eventTwo" so I tried doing it as a computed property, but it doesn't seem to work: 我似乎无法像on =“eventOne eventTwo”那样添加两者,所以我尝试将其作为计算属性,但它似乎不起作用:

<a {{action 'selectTab' 'location' on=clickEvent}} class="{{if locationSelected 'active'}}"><span class="mi-person"></span></a>
<a {{action 'selectTab' 'portfolios' on=clickEvent}} class="{{if portfolioSelected 'active'}}"><span class="mi-movie"></span></a>

export default Ember.Component.extend({
    session: Ember.inject.service(),
    clickEvent: Ember.computed('session.isCordova', function() {
        return this.get('session.isCordova') ? 'touchEnd' : 'mouseUp';
    }),
    actions: {
        selectTab:function(tab){
            console.log("TAB SELCTED");
            alert("TAB SELCTED");
            this.set('selectedTab', tab);
        }
    }
}

My other option is to duplicate it with an if statement, but I'd rather have an elegant solution. 我的另一个选择是用if语句复制它,但我宁愿有一个优雅的解决方案。

You can add both event types to your element, It will look like this: 您可以将两种事件类型添加到元素中,它将如下所示:

 <a {{action 'selectTab' 'location' on='touchEnd'}} {{action 'selectTab' 'location' on='mouseUp'}} class="{{if locationSelected 'active'}}">
      <span class="mi-person"></span>
    </a>
 <a {{action 'selectTab' 'portfolios' on='touchEnd'}} {{action 'selectTab' 'portfolios' on='mouseUp'}} class="{{if portfolioSelected 'active'}}">
   <span class="mi-movie"></span></a>


Source: Multiple actions in a single element 来源: 单个元素中的多个动作

您可以利用内联手柄帮助程序并以这种方式编写代码而不是计算属性。

<a {{action 'selectTab' 'location' on=(if session.isCordova 'touchEnd' 'mouseUp')}} class="{{if locationSelected 'active'}}"><span class="mi-person"></span></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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