Created
July 2, 2012 11:54
-
-
Save jincod/3032885 to your computer and use it in GitHub Desktop.
Backbone.js ctrl+left/right hot keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment