在我的应用程序中,我有一个ItemsService
从服务器获取项目并将它们作为 JSON 对象存储在其cache
变量中的。项目可以出现在许多地方,例如表格或图形/图表等。
例如,当我初始化 a table
- 我只需要从中选择特定项目cache
,例如 1st、3rd、7th。
如何实现它们之间的双向连接?基本上我想table
包含对特定项目的引用,cache
因此当我更改项目时cache
,table
它的状态将一直保持同步,因为它是同一个项目。
这是table
和cache
结构的示例:
桌子:
table: {
"36": { // it's a name of a row
"72": [items], // it's a name of a column with corresponding items
"73": [items],
"74": [items]
},
"37": {
"72": [],
"73": [items],
"74": [items]
},
"38": {
"72": [],
"73": [],
"74": []
}
}
ItemsService 缓存(简化版):
ItemsService = {
cache: [items]
};
物品结构:
{
id: 3,
parent_id: 1,
name: 'First Item',
siblings: [1,2,3],
active_users: [{user_id: 1, avatar_url: 'http://...'}, ...],
// 50 more fields :)
}
还需要指出我使用angular-ui-sortable
插件来允许在列/行之间拖动项目,并且我需要提供ng-model
数组(我认为)。这是它现在的样子:
<td ui-sortable="vm.sortableOptions"
ng-model="vm.table[row.id][column.id]">
<sb-item itemid={{item.id}}
ng-repeat="item in vm.table[row.id][column.id]">
</sb-item>
</td>