view.events

eventsview.events or view.events()
The events hash (or method) can be used to specify a set of DOM events that will be bound to methods on your View through delegateEvents.

Backbone will automatically attach the event listeners at instantiation time, right before invoking initialize.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var ENTER_KEY = 13;
var InputView = Backbone.View.extend({
 
  tagName: 'input',
 
  events: {
    "keydown" : "keyAction",
  },
 
  render: function() { ... },
 
  keyAction: function(e) {
    if (e.which === ENTER_KEY) {
      this.collection.add({text: this.$el.val()});
    }
  }
});
doc_Backbone
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.