隨着SPA應用程序的增長,有些事情在Angular JS中變得不清楚。假設我們有一個包含很多引用實體的RDBMS數據結構,比如Country(id),State(id,countryId),City(id,stateId),Street(id,cityId)。AngularJS RDBMS數據處理最佳實踐
我們需要在單個視圖中渲染大部分視圖,就像按照父節點遞歸分組的街道索引一樣。我知道這不是很實際的例子,特別是考慮到我們真正的應用程序實體具有我們需要綁定的相互數據。
「最佳實踐」將會如何?現在
,我們使用大量的NG-repeat的像這樣的:
<ul>
<li ng-repeat="country in countryList">
{{ country.name }}
<ul ng-if="(stateList | filter:{countryId === country.id}).length">
<li ng-repeat="state in filterStatesByCountryId(country.id)">
{{ state.name }}
<ul ng-if="(cityList | filter:{stateId === state.id}).length">
<li ng-repeat="city in cityList | filter:{stateId === state.id}">{{ city.name }}</li>
</ul>
</li>
</ul>
</li>
</li>
filterStatesByCountryId(country.id)
相同stateList | filter:{countryId === country.id}
。
我們的應用程序必須有適當的綁定,因此如果street.name
屬性在StreetService
中得到更新,該視圖反映了此更改。因此$scope.streetList = StreetService.all
不是開箱即用的選項,而是$scope.streetList
,我們將有$scope.street = StreetService
,然後將其用作$scope.street.all | filter:{cityId: city.id}
。
必須說這看起來很可怕。
什麼是最佳的性能和服務體系結構明智的呈現和顯示這種數據列表的方式?
想法之一是將整個合併的實體層次結構存儲到一個。因此那個擁有所有州,城市和街道的國家成爲一個對象。然後country
實體將擁有states {Array.<StateEntity>}
屬性,state {StateEntity}
將具有cities {Array.<CityEnity>}
屬性等。但是,如果單個street.name
會更新,我們需要遍歷整個country
數據對象以找到我們需要更新的街道,進行更新,然後重新 - 提供視圖,或者只是有一個$scope.watch(expr, fn, true)
這將是一個複雜的層次結構的矯枉過正。
謝謝。
P.S.我們無法切換到NoSQL來存儲數據。