emberjs + rails,捕获按键事件

时间:2013-06-23 03:50:43

标签: jquery ruby-on-rails ember.js keypress enter

我在普通的html文件中测试js脚本

$(document).ready ->
  $("#subject").keyup (event) ->
    alert "wll"

它有效。

但是当我在emberjs + rails项目中使用它时。 它不起作用

该怎么做?

2 个答案:

答案 0 :(得分:1)

有一条规则:您应该将所有事件添加到viewshttp://emberjs.com/guides/views/)。

你有所有活动的清单:

EVENT NAMES
Possible events names for any of the responding approaches described above are:

Touch events:

touchStart
touchMove
touchEnd
touchCancel
Keyboard events

keyDown
keyUp
keyPress
Mouse events

mouseDown
mouseUp
contextMenu
click
doubleClick
mouseMove
focusIn
focusOut
mouseEnter
mouseLeave
Form events:

submit
change
focusIn
focusOut
input
HTML5 drag and drop events:

dragStart
drag
dragEnter
dragLeave
drop
dragEnd

这是一个简单的示例,您可以如何使用它:

HTML

<script type="text/x-handlebars" data-template-name="application">
  {{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
  <div class='box'>Test</div>
</script>

JS

App = Ember.Application.create({});

App.IndexView = Ember.View.extend({
    // This is only for set the focus on our view.
    didInsertElement: function() {
      return this.$().attr({ tabindex: 1 }), this.$().focus();
    },
    keyPress: function(event) {
        console.log("KeyCode: " + event.keyCode);
    },
});

答案 1 :(得分:0)

使用视图您可以简单地执行此操作

AView = Ember.View.extend({
   keyUp: function(event){
       alert('wll');
   }
});