Search code examples
javascriptbackbone.jsmarionettebackbone-events

Access event object from Backbone Marionette ItemView trigger


I have the code below. I want to on <Enter> of the input#editTodo, I want to save the model (Todo). I figured I need to listen to the keypress event then check that the keycode is 13, if so I save the model ...

TodoView = Backbone.Marionette.ItemView.extend({
    triggers: {
        "keypress #editTodo": "detectEnterAndSave"
    },
    initialize: function() {
        this.on("detectEnterAndSave", function(e) {
            console.log(e);
        });

But I cant seem to get the event object? If so how do I know what key is pressed?


Solution

  • events: {
        'keypress #editTodo' : 'detectEnterAndSave'
    },
    
    detectEnterAndSave : function(e) {
      if (e.which === 13) {
        // do something
      }
    }