打字稿类型检查 - Backbone.Model实例无法进行类型检查 [英] Typescript type checking - Backbone.Model instance cannot be type checked

查看:210
本文介绍了打字稿类型检查 - Backbone.Model实例无法进行类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与打字稿和骨干集的问题。下面是一些基础:

I'm having a problem with Typescript and Backbone collections. 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();

 });

在渲染():

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});
      } 
      ....       
}

因为它似乎是类类型从 Backbone.Model 推导得到

除了的instanceof 永远的值为true分配到 Backbone.Model.attributes 而不是类本身的实例。我相信这是因为我的实现类,从Backbone.Model派生时,我跟着的这个做法,看起来它可能是这个问题的原因。基本上,它代表到底层Backbone.model.get()/()设置,进而设置在属性属性值对TS类/集的属性。

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. 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. 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.

看来我不得不要么放弃这一做法(希望这是问题),或者想出一个办法做类似或使用松散耦合式检查的instanceof baseChartModel.attributes 对象,我班原型 App.BackBone.Models.ScatterPlotModel

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 比较行

This breakpoint is at the line with instanceof comparison

任何想法?

推荐答案

这是您所使用的方法似乎很有效。我已经插入下面的code到操场打字稿只是检查了几个JavaScript的symantics:

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:

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/>' );
}

这给出了以下的输出:

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 

这必定意味着呼叫

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

在这里引起的问题。结果
可能scatterPlotCollection.models没有返回原来的对象(即ScatterPlotModel或BarChartModel),转而只返回[对象对象],是失败的,因此实例?

is causing the problem here.
Possibly scatterPlotCollection.models is NOT returning your original objects ( i.e. ScatterPlotModel or BarChartModel ), and is instead just returning [Object object], hence the instance of is failing ?

希望这有助于

这篇关于打字稿类型检查 - Backbone.Model实例无法进行类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆