为什么我的backbone.Collection只有一个元素? (使用Require,TypeScript,Jasmine)

时间:2017-09-15 11:43:13

标签: javascript typescript backbone.js requirejs

我是TypeScript和Jasmine的新手,对Backbone来说还是新手,并且需要这样我还在尝试将一些部分粘合在一起。

在TypeScript文件中的Jasmine测试中,我正在尝试定义backbone.Collection。这是我的代码(stilghtly refactored)

(...)

class ModelA extends backbone.Model {
    idAttribute: "N";
    constructor(N: number, StringPropertyA: string, StringPropertyB: string, NumericPropertyA: number, NumericPropertyB: number, DatePropertyA: Date, DatePropertyB : Date) {
        super();
        this.attributes.N = N;
        this.attributes.StringPropertyA = StringPropertyA;
        this.attributes.StringPropertyB = StringPropertyB;
        this.attributes.NumericPropertyA = NumericPropertyA;
        this.attributes.NumericPropertyB = NumericPropertyB;
        this.attributes.DatePropertyA = DatePropertyA;
        this.attributes.DatePropertyB = DatePropertyB;
    }
}
            //var OldModelA = backbone.Model.extend({
            //    idAttribute: "N",
            //    constructor: function (N, PropertyA, PropertyB) {
            //        this.N = N;
            //        this.PropertyA = PropertyA;
            //        this.PropertyB = PropertyB;
            //    }
            //});

            var ModelACollection = backbone.Collection.extend({model: ModelA});

            var i = 1;
            var model1 = new ModelA(i++, "Abc", "dfD9"),
                model2 = new ModelA(i++, "Cde", "gdkl"),
                model3 = new ModelA(i++, "Cdy", "grger"),
                model4 = new ModelA(i++, "Zly", "dzeersds"),
                model5 = new ModelA(i++, "Zlz", "sdfsfz");

            var modelArray = [model1, model2, model3, model4, model5];

            var collection1 = new ModelACollection({ models: modelArray });
            var collection2 = new backbone.Collection({ models: modelArray });

(...)

我希望collection1.modelscollection2.models是5个元素的数组,但这里是根据Chrome的内容:

我想我错过了什么......

Content of collection1 and collection2 according to Chrome's debugger

进一步搜索

类似的结果
        var collection3 = new backbone.Collection();
        collection3.add(model1);
        collection3.add(model2);
        collection3.add(model3);
        collection3.add(model4);
        collection3.add(model5);

1 个答案:

答案 0 :(得分:2)

你的模型定义是罪魁祸首,它无可救药地混淆Bac​​kbone:

  • 它错过了对父构造函数的调用以继承attributes属性。请参阅http://backbonejs.org/#Model-constructor
  • 中的第二个示例
  • 如果要直接编写属性,必须将它们存储在this.attributes.N = N; this.attributes.PropertyA = PropertyA; this.attributes.PropertyB = PropertyB; 哈希中,例如

    ModelA

鉴于这些点,var ModelA = Backbone.Model.extend({ idAttribute: "N", constructor: function (N, PropertyA, PropertyB) { Backbone.Model.call(this, { N: N, PropertyA: PropertyA, PropertyB: PropertyB }); } }); var m = new ModelA(1, "Abc", "dfD9"); console.log(m.toJSON()); 的可能定义,使用属性哈希调用父构造函数:

var collection1 = new ModelACollection(modelArray);

请注意,如果您没有覆盖构造函数,则可以使用模型数组创建集合作为第一个参数:

var ModelA = Backbone.Model.extend({
    idAttribute: "N",
    constructor: function (N, PropertyA, PropertyB) {
        Backbone.Model.call(this, {
            N: N,
            PropertyA: PropertyA,
            PropertyB: PropertyB
        });
    }
});

var i = 1;
var model1 = new ModelA(i++, "Abc", "dfD9"),
    model2 = new ModelA(i++, "Cde", "gdkl"),
    model3 = new ModelA(i++, "Cdy", "grger"),
    model4 = new ModelA(i++, "Zly", "dzeersds"),
    model5 = new ModelA(i++, "Zlz", "sdfsfz");
var collection1 = new Backbone.Collection([model1, model2, model3, model4, model5]);
console.log(collection1.toJSON());

和演示



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js"></script>
&#13;
{{1}}
&#13;
&#13;
&#13;