Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Uncaught TypeError: Object [object Object] has no method 'off'" error using events when moved to backbone 0.9.10

I has a view with this event:

var View = Backbone.View.extend({
  el: $('#test'),
  events: {
    "change input": "test"
  },
  test: function(e) {
    console.log("test");
  }
});

var view = new View();

With backbone 0.9.9 it work, but with backbone 0.9.10 I got this error: Uncaught TypeError: Object [object Object] has no method 'off'. What I need to change to work with events on backbone 0.9.10?

I'm using this cdn's

http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.10/backbone-min.js

Thanks

like image 355
Camilo Avatar asked Feb 13 '13 16:02

Camilo


2 Answers

I was using jquery 1.5.2 and the method off was introduced on jquery 1.7.0. Backbone 0.9.9 require that el contain the method unbind while Backbone 0.9.10 require that el contain the method off. The method unbind exist on jquery 1.5.2 and that was the reason why my code worked with backbone 0.9.9.

like image 179
Camilo Avatar answered Nov 12 '22 20:11

Camilo


Try this...

var Test = Backbone.View.extend({
  events: {
    "change input": "test"
  },

  initialize: function() {
    this.setElement($('#test'));
  },

  test: function(e) {
    alert("test");
  }
});

var test = new Test();

Or, this would be even better...

var Test = Backbone.View.extend({
  events: {
    "change input": "test"
  },

  test: function(e) {
    alert("test");
  }
});

var test = new Test({
  el: $('#test')
});
like image 25
DashK Avatar answered Nov 12 '22 18:11

DashK