1

我想更改emberjs 的 onEvent,它是执行关联视图的触发器action。onEvent 似乎只有 2 个选项: defaultenterkeypress. 我想知道我是否也可以有其他选择,例如focusOut.

小问题

但首先,我什至无法使用非默认选项:

Myapp.TextField = Ember.TextField.extend({
    onEvent: 'keypress'
});

文本字段没有响应按键,但继续响应enter

实际问题

我们如何让 ember.js 文本字段响应其他onEvents 来触发action视图中的指定。这是我所期待的:

哈佛商学院:

<script type="text/x-handlebars" id="themodel">
    {{view Myapp.TextField action="targetAction" valueBinding="myText"}}
</script>

JS视图:

Myapp.TextField = Ember.TextField.extend({
    // is this possible?
    onEvent: 'focusOut'
});

JS控制器:

Myapp.ThemodelController = Ember.ArrayController.extend({
    targetAction: function(){
        var usertext = this.get('myText');
        // do stuff with the usertext ...
    }
});

“实际问题”的解决方法

这是一种解决方法,因为它不修改onEvent,而是直接让focusOut触发 targetAction:

JS视图:

Myapp.TextField = Ember.TextField.extend({
    focusOut: function(){
        this.get('controller').set('myText', this.get('value')).targetAction();
    }
});

onEvent但我真的不喜欢这种繁琐的实现,所以如果有办法使用with ,请告诉我focusOut。谢谢。

4

2 回答 2

1

I experienced something similar to your "small problem", only instead of the text field only responding to enter, it would respond to neither keypresses nor enter.

The solution lies within mavilein's answer to this question - the word "keypress" must be in camelCase, i.e "keyPress". This applies whether it is used as a property name when extending the Ember.TextField class, as you had originally attempted, or as an attribute on the actual view element as Mike had suggested. I tried it both ways. Hope this helps.

于 2013-08-26T16:50:27.607 回答
0

但首先,我什至无法让非默认选项工作

奇怪的。对这种行为进行了测试。建议看看你的用例有什么不同。

我们如何让 ember.js 文本字段响应其他 onEvents 来触发视图中指定的操作。

遵循Ember.TextField 的 insertNewLine fx中使用的相同模式

Myapp.TextField = Ember.TextField.extend({
  onEvent: 'focusOut',
  focusOut: function() {
    sendAction('focusOut', this, event);
  }
});
于 2013-05-31T05:05:27.113 回答