注意:更改标题后答案以获得更好的可搜索性,因为这与骨干无关。
module App.BackBone.Collections {
export class MixedChartCollection extends Backbone.Collection {
public model: App.BackBone.Models.BaseChartModel;
constructor(models?: any, options?: any) {
super(models, options);
}
}
}
似乎使用this
上下文作为我的模块而不是我的类 o_O调用了基本 Backbone.Collection 构造函数
这是backbone.js的构造函数:
var Collection = Backbone.Collection = function (models, options) {
options || (options = {});
if (options.url) this.url = options.url;
if (options.model) this.model = options.model;
if (options.comparator !== void 0) this.comparator = options.comparator;
this._reset(); //ERROR: this._reset is not a function
this.initialize.apply(this, arguments);
if (models) this.reset(models, _.extend({ silent: true }, options));
};
在图像中,您可以看到与上下文App.BackBone.Collections
相同的 3 个成员(红色,其中一个是有问题的类)this
和 this._reset 最终未定义,因为我们真正希望它被“包装”我的一些无处不在的对象
为什么要这样做?
这是这个类的编译代码:
var App;
(function (App) {
(function (BackBone) {
(function (Collections) {
var MixedChartCollection = (function (_super) {
__extends(MixedChartCollection, _super);
function MixedChartCollection(models, options) {
_super.call(this, models, options); //HERE "this" is not MixedChartCollection instance
}
return MixedChartCollection;
})(Backbone.Collection);
Collections.MixedChartCollection = MixedChartCollection;
})(BackBone.Collections || (BackBone.Collections = {}));
var Collections = BackBone.Collections;
})(App.BackBone || (App.BackBone = {}));
var BackBone = App.BackBone;
})(App || (App = {}));