Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone bind event keydown to document returns Uncaught TypeError: Object [object Object] has no method 'apply'
    text
    copied!<p>I am having big trouble fixing this error. </p> <p>I have a view in Backbone.js and I want to bind some actions on it with keyboard events.</p> <p>Here is my view :</p> <pre><code>window.PicturesView = Backbone.View.extend({ initialize : function() { $(document).on('keydown', this.keyboard, this); }, remove : function() { $(document).off('keydown', this.keyboard, this); }, render : function(eventName) { // blah blah blah }, next : function() { // blah }, prev : function() { // blah }, keyboard : function(e) { console.log(e.keyCode); if (e.keyCode == 37) { this.prev(); return false; } if (e.keyCode == 39) { this.next(); return false; } } }); </code></pre> <p>When I press the keyboard, I get this error : <code>Uncaught TypeError: Object [object Object] has no method 'apply'</code> that is triggered by jQuery.</p> <p>After reading this : <a href="https://stackoverflow.com/questions/11032659/uncaught-typeerror-object-object-object-has-no-method-apply">Uncaught TypeError: Object [object Object] has no method &#39;apply&#39;</a></p> <p>I also tried : <code>$(document).on('keydown', function(e) { this.keyboard(e); }, this);</code> but it still gives me the same error.</p> <p>And : <code>$(document).on('keydown', 'keyboard', this);</code> in the <code>events : { 'event' : 'action' }</code> style but it doesn't do anything.</p> <p>I could probably find some hack by using jQuery only somewhere else in my code, but since I am not expert in event handling and Backbone, I want to run it the nice way.</p> <p>I hope I'm clear, thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload