我正在通過「掌握TypeScript」這本書,其中作者演示了使用Backbone生成模型,集合和視圖。Backbone,TypeScript和super
我已經定義了以下類:
export class ContactItemView extends Backbone.View<cm.ContactModel> {
template: (properties?: any) => string;
constructor(options?: any) {
this.className = "contact-item-view";
this.template = _.template(contactItemSnippet);
this.events = <any>{ 'click': this.onClicked };
super(options);
}
...
}
不幸的是,打字稿不會錯誤編譯如下:
Build: 'super' must be called before accessing 'this' in the constructor of a derived class
然而,如果我將調用超級上述「這個」
export class ContactItemView extends Backbone.View<cm.ContactModel> {
template: (properties?: any) => string;
constructor(options?: any) {
super(options);
this.className = "contact-item-view";
this.template = _.template(contactItemSnippet);
this.events = <any>{ 'click': this.onClicked };
}
...
}
然後我的事件不會觸發。我唯一能做的就是解決這個問題,將調用改爲super,在生成的JavaScript代碼中使用「this」,從而修改TypeScript編譯的內容。
有沒有辦法讓我的事件工作,同時仍然堅持正確的TypeScript規則?
值得一讀:http://benmccormick.org/2015/04/07/es6-classes-and-backbone-js/ –
@RyanCavanaugh,爲感謝鏈接。這是一個很好的閱讀。由於這只是一個教程,我並不真正擔心它。我只是好奇我是否可以把它工作。看來,使用Backbone,我將無法得到這個工作。大多數;我計劃使用Angular。 –