I'm writing a Backbone program in Typescript, in which I am not able to initialize any events. Here's a test class that I've created to fix the problem. The function start() is not being called when the div is clicked on.
class TestView extends Backbone.View{
events = {
"click #testDiv" : "start"
}
start(){
console.log("Clicked");
}
constructor(options?){
super(options);
}
render(){
$root.html(getNewDiv("testDiv"));
$("#testDiv").css("height", 100).css("width", 100).css("background-color", "green");
console.log("Rendered");
return this;
}
}
function getNewDiv(id:string) {
return "<div id = \"" + id + "\"></div>"
}
new TestView().render();
Here's the console output:
Rendered
Here's the backbone typescript definition that I'm using:
https://github.com/borisyankov/DefinitelyTyped/blob/master/backbone/backbone.d.ts
Here's the CDN location for backboneJS
Minified : http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js
Non-Minified : http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone.js
I'm concerned if my syntax is right or this has something to do with the Typescript definition of Backbone.
UPDATE
The answer shown by Kevin Peel below is throwing an error, because in the Typescript definition file, "events" is defined as a function ( eg. events()). If I just create an "events" function, I get the error - "Uncaught TypeError: Object [object Object] has no method 'off'". If I use the getter method (eg. get events(){}), then nothing really happens on actually firing the event.
Using delegate Events
I tried to use the delegateEvents() function in Backbone.View which creates an error:
constructor(options:any, question:Question, div:JQuery) {
this.delegateEvents({
"click" : "clicked"
});
}
Error:
Uncaught TypeError: Object [object Object] has no method 'off' backbone.js:1082
h.extend.undelegateEvents backbone.js:1082
h.extend.delegateEvents backbone.js:1059
getNewDivis or does... - Kevin Peel$rootthe same as$el? because in order for the click event to be fired the#testDivmust be a descendent of the viewsel- Jack