qeslvg
Last Updated: February 25, 2016
·
1.676K
· jincod

Backbone.js ctrl+left/right hot keys

var eventObject = {};
_.extend(eventObject, Backbone.Events);

//http://jsfiddle.net/vBh47/8/
(function (codes, eventObject, code, evt) {

    document.addEventListener && // Modern browsers only
    document.addEventListener("keydown", function (e) {
        code = codes[e.keyCode];
        if (e.ctrlKey || e.metaKey && code) {
            if(code)
                {
                    eventObject.trigger(code);
                    console.log("sends");
                }
        }
    }, false);

}({37: "prev", 39: "next"}, eventObject));

var View = Backbone.View.extend({

    initialize : function (options) {
        this.eventObject = options.eventObject;
        this.eventObject.on("next", this.nextPage, this);
        this.eventObject.on("prev", this.prevPage, this);
    },
    prevPage : function(){
        alert("ctrl+left");
    },
    nextPage : function(){
        alert("ctrl+right");
    },
    render : function() {
        $(this.el).html(this.template());
        return this;
    }    
});

var view = new View({ eventObject : eventObject });
view.render();

//demo: http://jincod.tumblr.com/hotkeys

1 Response
Add your response

15394

That is not THE jQuery equivalent, @mechax. You would want to use the normalized event.which.

over 1 year ago ·