user1187135
user1187135

Reputation:

How do you listen to keydown on Backbone events method

What is the equivalent of this $(document).on('keydown', this.logKey); for Backbone events:{}? I am asking because I rather bind all events at once place rather than put some in the initialize method.

I tried the suggestion here: how to capture the key event from a view ? I.e. events{'keydown' : 'logKey'}. This is not working for me though.

Upvotes: 3

Views: 3237

Answers (1)

fguillen
fguillen

Reputation: 38832

The CSS selectors defined in the View.delegateEvents() are bounded to the context of View.$el so if you want the View to manage the events on document the View.$el should be document itself:

// code no tested
var DocumentView = Backbone.View.extend({
  el: "document",
  events: {
    "keydown": "theHandler"
  }
})

Upvotes: 5

Related Questions