为什么我的 backbone.Collection 只有一个元素? (使用 Require、TypeScript、Jasmine)
Why does my backbone.Collection have only one element? (using Require, TypeScript, Jasmine)
我是 TypeScript 和 Jasmine 的新手,对 Backbone 和 Require 还是相当陌生,所以我仍在尝试将一些部分粘合在一起。
在 TypeScript 文件中的 Jasmine 测试中,我试图定义一个 backbone.Collection
。这是我的代码(经过简单重构)
(...)
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.models
和 collection2.models
是 5 个元素的数组,但根据 Chrome 以下是它们的内容:
我想我遗漏了什么...
进一步搜索
与
相似的结果
var collection3 = new backbone.Collection();
collection3.add(model1);
collection3.add(model2);
collection3.add(model3);
collection3.add(model4);
collection3.add(model5);
你的模型定义是罪魁祸首,它无可救药地混淆了 Backbone:
- 它错过了对父构造函数的调用以继承
Backbone.Model
属性。请参阅 http://backbonejs.org/#Model-constructor 中的第二个示例
如果要直接写属性,必须存储在attributes
散列中,例如
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>
我是 TypeScript 和 Jasmine 的新手,对 Backbone 和 Require 还是相当陌生,所以我仍在尝试将一些部分粘合在一起。
在 TypeScript 文件中的 Jasmine 测试中,我试图定义一个 backbone.Collection
。这是我的代码(经过简单重构)
(...)
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.models
和 collection2.models
是 5 个元素的数组,但根据 Chrome 以下是它们的内容:
我想我遗漏了什么...
进一步搜索 与
相似的结果 var collection3 = new backbone.Collection();
collection3.add(model1);
collection3.add(model2);
collection3.add(model3);
collection3.add(model4);
collection3.add(model5);
你的模型定义是罪魁祸首,它无可救药地混淆了 Backbone:
- 它错过了对父构造函数的调用以继承
Backbone.Model
属性。请参阅 http://backbonejs.org/#Model-constructor 中的第二个示例
如果要直接写属性,必须存储在
attributes
散列中,例如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>