在我的應用程序中,我有一個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>
你應該向我們展示兩個數組的結構,因爲有很多方法可以做到這一點,這取決於它們的結構。 – Buzinas
我提供了結構 – yaru
'items'結構是最重要的。 – Buzinas