我在ember v2.3.0中检测按键时遇到了问题。我是一个初学者,尝试编写一个简单的组件来显示按下的键。但是我无法运行操作并使用参数。
基本上,我可以在didRender中使用
this.$().on("keypress",function(){ ... });
获取keyCode,但我认为在Ember中这不是一个好的实践。有人能帮帮我吗?谢谢:)
以下是我的代码:
http://emberjs.jsbin.com/vucenomibo/edit?html,js,output
HTML: <script type="text/x-handlebars" data-template-name="application"></script>
<script type="text/x-handlebars" data-template-name="index"></script>
<script type="text/x-handlebars" data-template-name="components/my-component"></script>
JS:
App = Em.Application.create();
App.IndexController = Em.Controller.extend({
actions : {
}
});
App.MyComponentComponent = Em.Component.extend({
didRender: function() {
console.log(this.get('context'));
console.log(this.get('item'));
return this.$().attr({ tabindex: 1 }), this.$().focus();
},
pressedKeyCode: null,
actions:{
smth: function(e) {
console.log('aaa');
this.send('displayKey', String.fromCharCode(e.keyCode));
},
displayKey: function(keyCode) {
this.get('item').set('pressedKeyCode', keyCode);
}
}
});
编辑
我把它做成了这样:http://emberjs.jsbin.com/qipeponiqu/1/edit?html,js,output
有人能帮忙审查一下吗?我想学习最佳实践。
{{action ... parameters ... on="keypress"}}
。 - nem035setAction="updateKey"
但似乎没有对它进行任何操作? - locks