我有一个集合,当获取它时得到一个json并放入集合: JSON格式是:
[
{
name: 'Hello',
age: '22',
bio: [{
interest: 'soccer',
music: 'r&B'
}]
}
]我想做另一个收集从生物(而不是再次抓取)。原因是我想同时访问姓名、年龄和bio,而解析函数只能返回一次?
var user = new Backbone.Collection.extend({
url: '/user',
parse: function (response) {
//I want both of this?
//return response;
return response.bio;
}
});我将此集合传递到两个不同视图的成功函数上。
//Controller File....
.............
mycollection.fetch({
success: function() {
//Details View wants response
PrimaryLayout.main.show(new detailsView{collection: mycoll});
//Bio View wants response.bio
PrimaryLayout.body.show(new bioView{collection: mycoll});
}
})解决这个问题的最好方法是什么?我能克隆一个收藏并在里面放上生物吗?
发布于 2014-01-10 18:51:49
我通常通过在parse中实例化子集合来解决这个问题。
var User = Backbone.Model.extend({
parse: function(json) {
// instantiate the collection
json.bio = new Backbone.Collection(json.bio);
// now person.get('bio') will return a Collection object
return json;
}
});
var Users = Backbone.Collection.extend({
model: User,
// ...
});发布于 2014-01-10 08:16:14
我想这就是你要找的:骨干协会。看看那里的教程和例子。
https://stackoverflow.com/questions/21034136
复制相似问题