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.

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
2016-04-17 12:21:50
Comments
Leave a Comment

Please login to continue.