angular - @ngrx with API, how do I recognise differences in results?
问题描述
I've stumbled into a problem and I feel that it's getting worse as time goes. The problem I'm having is that API-calls might give same results but with slightly different attributes, and I'm having a problem to figure out how to implement this with @ngrx.
Example:
- We've got a user role:
system user
.- Non-logged-in-users uses
/api/activities
to fetch activities. System user
uses/api/su/activities
to fetch activities.- Both API-calls return activities but one of them also returns an attribute
activity_categories
.
- Non-logged-in-users uses
- Before
SU
is logged in they fetch activities with/api/activities
and the result is stored in the store. - Then
SU
logs in and now needs the extra data to each activity (activity_categories) and checks if the activities are already fetched. If they are it skips the API call (/api/su/activities
). SU
now gets an error because each activity is missing their categories (activity['activity_categories']).
So, is there a smart way to get round this? :)
解决方案
对此有两种可能的解决方案。最简单的方法是忽略这些是“相同”数据这一事实,并将两组不同的活动数据(一组丰富,一组不丰富)作为完全独立的事物存储在状态存储中。
另一种选择是重构 API 以提供一个端点,该端点仅返回活动的额外类别数据(例如/api/su/activitiycategories
),然后如果您已经拥有该状态中的基本活动数据,则仅调用该第二个端点。然后,您将使用额外的类别数据更新状态数据。
您可能需要在状态中存储一个标志(例如“activitiesEnrichedWithCategories”)来控制您是否需要进行该调用。在基本活动不存在的情况下,您需要同时调用这两个函数来获取丰富的数据,或者/api/su/activitycategories/
改为调用端点。
第二个选项实现起来要复杂得多,但如果有很多活动,那么额外的努力可能是值得的。
推荐阅读
- c++ - 是什么导致 macOS Mojave 上的 GoogleTest 链接错误?
- acumatica - Acumatica,修改从发票生成的 Gl 批次以处理多付款
- jquery - 打开灯箱后将 data-textval 中的参数添加到 URL
- excel - 将筛选器添加到 Excel 中的 Publisher 邮件合并
- angular - 为什么 Angular Material 元素在页面中不可见?
- javascript - Webpack 4 动态导入导致 TypeError: [object Module] is not a constructor
- c++ - 是否有任何理由不会将堆指针分配给数组?
- angular - 如何为 Tomcat 配置 Nginx proxy_pass in
- reactjs - Uncaught (in promise) TypeError: loginMutation is not a function
- java - 无限物品recyclerview练习