【发布时间】:2015-12-24 20:24:31
【问题描述】:
在我的应用程序中,我有一个 ItemsService,它从服务器获取项目并将它们作为 JSON 对象存储在其 cache 变量中。物品可以出现在许多地方,例如在表格或图形/图表等中。
例如,当我初始化 table 时 - 我只需要从 cache 中选择特定项目,例如第一,第三,第七。
如何实现它们之间的双向连接?基本上我希望table 包含对来自cache 的特定项目的引用,所以当我在cache 或table 中更改项目时 - 它的状态将一直保持同步,因为它是相同 物品。
当我从table 中删除项目时,它也需要从cache 中删除。
这是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>
【问题讨论】:
-
你应该向我们展示两个数组的结构,因为有很多方法可以做到这一点,这将取决于它们的结构。
-
我提供了结构
-
items结构是最重要的。 -
项目是一个复杂的对象。将在几分钟内提供它的示例。
标签: javascript angularjs jquery-ui-sortable angular-ui-sortable