1

注意:更改标题后答案以获得更好的可搜索性,因为这与骨干无关。

 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 = {}));          

在此处输入图像描述

4

2 回答 2

2

我假设你有一个这样声明的 BaseChartModel:

module App.Backbone.Models {
    export class BaseChartModel extends Backbone.Model {

    }
}

还有你的 MixedChartCollection:

module App.Backbone.Collections {
    export class MixedChartCollection extends Backbone.Collection {
        public model: App.Backbone.Models.BaseChartModel;
        constructor(models?: any, options?: any) {
            super(models, options);
        }
    }
}

我看到的一个关键问题是您有一个名称空间,包括Backboneand Backbone。当这些转换为 JavaScript 时:

var App;
(function (App) {
    (function (Backbone) {
        (function (Models) {
            var BaseChartModel = (function (_super) {
                __extends(BaseChartModel, _super);
                function BaseChartModel() {
                    _super.apply(this, arguments);

                }
                return BaseChartModel;
            })(Backbone.Model);
            Models.BaseChartModel = BaseChartModel;            
        })(Backbone.Models || (Backbone.Models = {}));
        var Models = Backbone.Models;
    })(App.Backbone || (App.Backbone = {}));
    var Backbone = App.Backbone; // << scope chain now includes Backbone
})(App || (App = {}));
var App;
(function (App) {
    (function (Backbone) {
        (function (Collections) {
            var MixedChartCollection = (function (_super) {
                __extends(MixedChartCollection, _super);
                function MixedChartCollection(models, options) {
                    _super.call(this, models, options);
                }
                return MixedChartCollection;
            })(App.Backbone.Collection);
            Collections.MixedChartCollection = MixedChartCollection;            
        })(Backbone.Collections || (Backbone.Collections = {}));
        var Collections = Backbone.Collections;
    })(App.Backbone || (App.Backbone = {}));
    var Backbone = App.Backbone;  // << scope chain now includes Backbone
})(App || (App = {}));

您会发现最终Backbone设置为冲突App.Backbone(生成代码的倒数第二行)。

var Backbone = App.Backbone; // << scope chain now includes Backbone

这完全改变了代码的性质,因为现在继承发生在本地范围的Backbone实例值而不是 BackboneJSModel类(类也是如此Collection)。

尝试将模块声明更改为不为您的集合和模型类使用 Backbone 命名空间。

module App.My.Models {
    export class BaseChartModel extends Backbone.Model {

    }
}

module App.My.Collections {
    export class MixedChartCollection extends Backbone.Collection {
        public model: App.My.Models.BaseChartModel;
        constructor(models?: any, options?: any) {
            super(models, options);
        }
    }
}
于 2013-03-30T14:42:30.327 回答
2

好的,想通了。

这与我实例化类的方式有关:

 var mixedCollection: App.BackBone.Collections.MixedChartCollection
                        = App.BackBone.Collections.MixedChartCollection();

只是注意到我遗漏了导致我的问题的“新”关键字。

为什么编译器没有捕捉到这个?我不知道。没有必要,但如果有人想要回答,那么背后的推理将标记为答案。

于 2013-03-30T20:19:47.497 回答