簡體   English   中英

EmberView 'keydown' 和 'keypress' 事件不呈現?

[英]EmberView 'keydown' and 'keypress' events do not render?

我有一個 EmberApp,它有一個keyDown事件,我打算在呈現特定視圖時提供它,讓我們稱之為PostsCommentView ,為此,我這樣做:

App.PostsCommentView = Ember.View.extend({
   eventManager:Ember.Object.extend({
      keyDown:function(event,view){
             console.log("keydown");  // I CAN see this
      },
      click: function(event,view){
              console.log("clicked"); // I CAN see this
      }
   })
    didInsertElement:function(){
              console.log("I have been inserted"); // I CAN see this
    }

});

正如您可以從我的代碼中讀到的那樣,該視圖確實會插入,並且click事件也會將“單擊”記錄到控制台中,但不適用於keydown事件。

我四處搜索,似乎我需要將焦點設置在“視圖”上,以便注冊keyDown事件。 所以我將它添加到我的didInsertElement函數中:

this.$().focus();

然而,這也不起作用。 附帶說明一下,我將keyDown事件添加到擴展Ember.TextArea的 Textarea 視圖中,並且它確實注冊了keyDown事件。

我在這里缺少什么?

所以我想出了什么問題,答案在於'keydown'事件不能被觸發,除非主要元素關注它。 當然,正如我的代碼指定的那樣,這可以解決

this.$().focus();

但是,它深入 1 級。 這個 EmberView 渲染的默認元素是一個 div,它必須是一個 div。 除非您明確使用 'tabindex' 屬性,將其設置為 -1(不能被制表符)或 0(可以被制表符並且是可聚焦的),否則 Div 不能被“聚焦”。

所以我在我視圖的 didInsertElement 中做了這個

didInsertElement(){
   this.$().attr('tabindex',0);
   this.$().focus();
},
keyDown:function(){
   console.log('keydown function was triggered') ; // This time it logs itself fine 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM