如何在车把输入组件的动作中传递参数?使用 Ember.js

How pass parameter in action of handlebars input component? Using Ember.js

我需要使用这个输入组件:

{{input type="textarea" value=fileName class="field" action="setFileName" on="key-press"}}

但是我要传一个参数

我不想做:

<input type="textarea" {{bind-attr value=fileName}} class="field" {{action 'setFileName' param on="keyPress"}}

您可以通过以下方式扩展内置组件来执行您想要的操作:

export default Ember.TextField.extend({
    sendAction: function(name) {
        if (name === 'action') {
            return this._super('action', this.get('value'), this.get('otherArgument'));
        } else {
            return this._super.apply(this, arguments);
        }
    }
});

然后你可以像这样使用它:

{{custom-input otherArgument=someValue action='callback'}}

然后,执行此操作以捕捉动作:

actions: {
    callback: function(value, otherArgument) {
        // value is the text in the input
        // otherArgument is the `someValue` that you passed in
    }
}