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
Written by Abdrashitov Vadim
Related protips
1 Response
That is not THE jQuery equivalent, @mechax. You would want to use the normalized event.which.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Backbone.js
Authors
Related Tags
#backbone.js
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#