简体   繁体   English

打字稿类型检查 - 无法对Backbone.Model实例进行类型检查

[英]Typescript type checking - Backbone.Model instance cannot be type checked

I'm having a problem with Typescript and Backbone collections. 我遇到了Typescript和Backbone集合的问题。 Here's some groundwork: 这是一些基础:

class BaseChartModel extends Backbone.Model { }
class ScatterPlotModel extends BaseChartModel { }
class BarChartModel extends BaseChartModel { }

class MixedChartCollection extends Backbone.Collection { public model: BaseChartModel; ... }
class ScatterPlotCollection extends Backbone.Collection { public model: ScatterPlotModel; ... }
class BarChartCollection extends Backbone.Collection { public model: BarChartModel; ... }

Then I do the following: 然后我做以下事情:

 var scatterPlotCollection = new ScatterPlotCollection();
 var scatterPlotCollectionXhr = scatterPlotCollection.fetch({...});

 var barChartCollection = new BarChartCollection();
 var barChartCollectionXhr = barChartCollection.fetch({...});

 $.when(scatterPlotCollectionXhr, barChartCollectionXhr).done(() => {

       mixedCollection = new MixedChartCollection();
       mixedCollection.add(scatterPlotCollection.models, { silent: true });
       mixedCollection.add(barChartCollection.models, { silent: true });
       chartViewList = new MixedChartViewList({ collection: mixedCollection });

       chartViewList.render();

 });

Inside render(): 里面的渲染():

public render(){
     var self = this;
     this.$el.html(this.template({}));

     this.collection.forEach(
    (chartModel: BaseChartModel) => {
            this.$el.append(self.AddChartView(chartModel).render());                  
    }
 );

     return this;
}

public AddChartView(baseChartModel: BaseChartModel) : BaseChartView {

      var newChartView: BaseChartView;

      if(baseChartModel instanceof ScatterPlotModel){
           newChartView = new ScatterPlotView({ model: baseChartModel});
      } else if (baseChartModel instanceof BarChartModel){
           newChartView = new BarChartView({ model: baseChartModel});
      } 
      ....       
}

Except instanceof never evaluates to true because it seems that the 'class type' deriving from Backbone.Model gets assigned to Backbone.Model.attributes rather than being an instance of the class itself. 除了instanceof永远不会计算为true,因为似乎从Backbone.Model派生的'类类型'被分配给Backbone.Model.attributes而不是类本身的实例。 I believe this is because when implementing my classes that derive from Backbone.Model I followed this approach and it seems like it may be the reason for this problem. 我相信这是因为在实现从Backbone.Model派生的类时,我遵循这种方法,似乎可能是这个问题的原因。 Basically it delegates get/set properties on the TS class to the underlying Backbone.model.get()/set() which in turn sets the values on the attributes property. 基本上,它将TS类的get / set属性委托给底层的Backbone.model.get()/ set(),后者又设置attributes属性的值。

It seems I have to either drop this approach (and hope that was the problem) or figure out a way to do a loosely coupled type check similar to or using instanceof between the baseChartModel.attributes object and my class prototype for App.BackBone.Models.ScatterPlotModel 看来我不得不要么放弃这一做法(希望这是问题),或者想出一个办法做类似或使用松散耦合的类型检查instanceof之间baseChartModel.attributes对象和我的类原型App.BackBone.Models.ScatterPlotModel

This breakpoint is at the line with instanceof comparison 此断点位于instanceof比较的行 在此输入图像描述

Any ideas? 有任何想法吗?

The approach that you are using seems valid enough. 您使用的方法似乎足够有效。 I have plugged the following code into the TypeScript playground just to check a few javascript symantics: 我已将以下代码插入到TypeScript游戏中,只是为了检查一些javascript语法:

class A { }

class B extends A { }

class Checker {
    static isA(input: A) {
        return (input instanceof A);
    }
}

var a = new A();
var b = new B();

document.writeln('a instanceof A ' + (a instanceof A) + '<br/>');
document.writeln('b instanceof B ' + (b instanceof B) + '<br/>');
document.writeln('b instanceof A ' + (b instanceof A) + '<br/>');
document.writeln('b instanceof A (via function) ' + Checker.isA(b) + '<br/>');

var aArray : A[] = [];

aArray.push(a);
aArray.push(b);

for(var i = 0; i < aArray.length; i++) {
    document.writeln('aArray[' + i + '] instance of A ' + (aArray[i] instanceof A) + '<br/>' );
}

This gives the following output: 这给出了以下输出:

a instanceof A true 
 b instanceof B true 
 b instanceof A true 
 b instanceof A checker true 
 aArray[0] instance of A true 
 aArray[1] instance of A true 

This must mean that the call 这必须意味着通话

mixedCollection.add( scatterPlotCollection.models, { silent: true });

is causing the problem here. 在这里导致问题。
Possibly scatterPlotCollection.models is NOT returning your original objects ( ie ScatterPlotModel or BarChartModel ), and is instead just returning [Object object], hence the instance of is failing ? 可能scatterPlotCollection.models没有返回原始对象(即ScatterPlotModel或BarChartModel),而只是返回[Object object],因此实例失败了?

Hope this helps, 希望这可以帮助,

I guess I know where a 'bug': somehow declaration 我想我知道'bug'在哪里:不知怎的声明

public model: CustomModelType

doesn't work; 不起作用;

But, if you will create a collection like this: 但是,如果你要创建一个这样的集合:

 new MyCustomCollection(null, { model: CustomModelType });

Then backbone will be able to create a models as expected. 然后骨干将能够按预期创建模型。

Why it happens, I don't know :( May be a bug in type definition. 为什么会发生,我不知道:(可能是类型定义中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM