It seems like a keystroke can only be performed on a focus element? I do not completely agree with this, should there be a way to execute a keypress event similar to a click event?
I have a view that works with one element at a time. I have a mouseenter function - mouseleave , which adds a class to the element that the mouse is finished with. When an element receives this class, I want to be able to use a keypress event to trigger a function for this element.
Obviously this is a small hurdle, but I'd like to figure out what I need to do. The following is an example.
var PlayerView = Backbone.View.extend({ tagName: 'div', events: { 'click .points, .assists, span.rebounds, span.steals':'addStat', 'mouseenter': 'enter', 'mouseleave': 'leave', 'keypress': 'keyAction' }, enter: function() { this.$el.addClass('hover'); }, leave: function() { this.$el.removeClass('hover'); }, keyAction: function(e) { var code = e.keyCode || e.which; if(code == 65) { alert('add assist') } } });
So, there isnโt much logic here, but I think Iโll write something like this
keyAction: function(e) { var code = e.keyCode || e.which; if(code == 65) { var addAssist = parseInt(this.model.get('assists')) + 1; this.model.save({assists: addAssist}); } }
Basically, if I could figure out how to run this keyAction method, I should be good. So, what are some of the caveats that I miss when executing some code? I am sure there are several.
I understand what is wrong with this code, it doesnโt know when we run keypress in this view, I will need to add a conditional or something to find the active class, so when I execute keypress it knows which model I say a very vague description here, but I realized that something is wrong, I'm just not sure how to do this?
My decision
initialize: function() { this.listenTo(this.model, "change", this.render); _.bindAll(this, 'on_keypress'); $(document).bind('keydown', this.on_keypress); }, enter: function(e) { this.$el.addClass('hover'); }, leave: function(e) { this.$el.removeClass('hover'); }, on_keypress: function(e) { // A for assist if(e.keyCode == 65) { if(this.$el.hasClass('hover')) { var addThis = parseInt(this.model.get('assists')) + 1; this.model.save({assists: addThis}); } } // R for rebound if(e.keyCode == 82) { if(this.$el.hasClass('hover')) { var addThis = parseInt(this.model.get('rebounds')) + 1; this.model.save({rebounds: addThis}); } } // S for steal if(e.keyCode == 83) { if(this.$el.hasClass('hover')) { var addThis = parseInt(this.model.get('steals')) + 1; this.model.save({steals: addThis}); } } // 1 for one point if(e.keyCode == 49) { if(this.$el.hasClass('hover')) { var addMake = parseInt(this.model.get('made_one')) + 1; this.model.save({made_one: addMake}); var addOne = parseInt(this.model.get('points')) + 1; this.model.save({points: addOne}); } } // 2 for two points if(e.keyCode == 50) { if(this.$el.hasClass('hover')) { var addMake = parseInt(this.model.get('made_two')) + 1; this.model.save({made_two: addMake}); var addTwo = parseInt(this.model.get('points')) + 2; this.model.save({points: addTwo}); } } // 2 for two points if(e.keyCode == 51) { if(this.$el.hasClass('hover')) { var addMake = parseInt(this.model.get('made_three')) + 1; this.model.save({made_three: addMake}); var addThree = parseInt(this.model.get('points')) + 3; this.model.save({points: addThree}); } } }
This is cool for my application, because when a user hovers over an item, the user can press a key to add data, rather than click.