我基本上正在尝试解决这个问题,但是使用数组并使用相应数组的值返回所有对象,而不仅仅是值:按属性值过滤骨干集合 https://stackoverflow.com/questions/11762105/filter-backbone-collection-by-attribute-value
我的本能是在人员集合上使用 _.filter 或 _.partition,但我对如何比较和返回这些内容感到困惑,并且没有得到所需的结果。
通过以下方式,通过路由器传递 id:
friendsRoute: function(id){
persons = new App.Collections.Persons();
persons.fetch().done(function(){
var single = persons.find(function(i){
return i.get('id') == id;
});
var friendsIds = single.get('friends');
var friends = ?
//var friendsList = new App.Views.FriendsList();
//friendsList.render({ friends: friends });
});
},
我有一个 FriendsIds 数组(乔的朋友):
friendsIds = [1,4,5]
并尝试从以下 JSON 中获取匹配的 ID 并在我的 FriendsList 视图中显示他们的名字:
[
{ "id": 1, "name": "Steve", "age": 22, "friends": [4] },
{ "id": 2, "name": "Mary", "age": 18, "friends": [1,3] },
{ "id": 3, "name": "Joe", "age": 43, "friends": [1,4,5] },
{ "id": 4, "name": "Tommy", "age": 19, "friends": [1] },
{ "id": 5, "name": "Leslie", "age": 27, "friends": [2,4] }
]