I am writing a Backbone program in Typescript in which I cannot initialize any events. Here is a test class that I created to fix the problem. The function is start()not called when the div is clicked.
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 is the typescript baseline definition that I use:
https://github.com/borisyankov/DefinitelyTyped/blob/master/backbone/backbone.d.ts
Here is the CDN place 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 worried if my syntax is right, or does this have anything to do with the typescript Backbone definition.
UPDATE
, , , typescript "" (, events()). "", "Uncaught TypeError: Object [object Object] " off ". getter (, get events() {}), .
Using delegate Events
delegateEvents() Backbone.View, :
constructor(options:any, question:Question, div:JQuery) {
this.delegateEvents({
"click" : "clicked"
});
}
:
Uncaught TypeError: Object [object Object] has no method 'off' backbone.js:1082
h.extend.undelegateEvents backbone.js:1082
h.extend.delegateEvents backbone.js:1059