NGRX - how to have calculated properties on items in the store

孤者浪人 提交于 2019-12-11 09:08:28

问题


We're using NGRX in our Angular applications. Data is coming from an API. Certain properties come from the API as an unformatted string, so we need to format this. Of course this can be done in the HTML, but the problem is that the value is required in multiple locations, in both HTML as well as in TypeScript.

So, initially we created getters for these display properties on our domain objects, but it is not supported by ngrx to store complex objects in the store as they are not serializable. ngrx should store simple JavaScript interfaces.

I don't know what the proper solution is, some options I had in mind: - In the reducer, when the entities are updated/added, populate the display properties. - In the selector, when the entities are retrieved, enrich them with the display properties. - Have effects that is triggered every time an entity is updated/added to enrich them with the display properties.

Every solution requires that the reducer/selector/effect transforms / enriches the input by calling a function. If that's forgotten, the display data is outdated/missing. It just does not feel right.

Any other suggestions please?

来源:https://stackoverflow.com/questions/54581532/ngrx-how-to-have-calculated-properties-on-items-in-the-store

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!