backbone 要查看模板的集合
backbone collection to view to template
我正在连接到第 3 方 API,returns 一个包含数组的对象。
我正在尝试将其放入 backbone 集合,然后将其通过管道输出到视图。
我尝试了很多东西,最近的是像这样简单的东西:
var MyCollection = Backbone.Collection.extend({
url: '/api/data',
parse: function (resp) {
return JSON.parse(resp);
},
});
var myCollection = new MyCollection();
myCollection.fetch();
return Backbone.View.extend({
template: _.template(tmpl),
render: function() {
this.$el.html(this.template({
coll: myCollection.toJSON()
}));
return this;
}
这只是在我的模板中给我 [Object Object]。
如果我将它写到控制台,我只会看到:
YourCollection
[Object]
yourdata.metadata: "www.xyz.edu/"
value: Array[3]
0: Object
Id: "000"
Name: "Name0"
IsValid: True
1: Object
ID: "111"
Name: "name1"
IsValid: True
3: Object
ID: "222"
Name: "name2"
IsValid: True
如果我能将每个数组元素放入它自己的模型中就好了,但我不确定该怎么做。
谢谢!
似乎您需要在 parse
方法中过滤实际集合:
function (resp) {
return JSON.parse(resp).value;
}
我正在连接到第 3 方 API,returns 一个包含数组的对象。
我正在尝试将其放入 backbone 集合,然后将其通过管道输出到视图。
我尝试了很多东西,最近的是像这样简单的东西:
var MyCollection = Backbone.Collection.extend({
url: '/api/data',
parse: function (resp) {
return JSON.parse(resp);
},
});
var myCollection = new MyCollection();
myCollection.fetch();
return Backbone.View.extend({
template: _.template(tmpl),
render: function() {
this.$el.html(this.template({
coll: myCollection.toJSON()
}));
return this;
}
这只是在我的模板中给我 [Object Object]。
如果我将它写到控制台,我只会看到:
YourCollection
[Object]
yourdata.metadata: "www.xyz.edu/"
value: Array[3]
0: Object
Id: "000"
Name: "Name0"
IsValid: True
1: Object
ID: "111"
Name: "name1"
IsValid: True
3: Object
ID: "222"
Name: "name2"
IsValid: True
如果我能将每个数组元素放入它自己的模型中就好了,但我不确定该怎么做。
谢谢!
似乎您需要在 parse
方法中过滤实际集合:
function (resp) {
return JSON.parse(resp).value;
}