合并 2 个 redux 存储的状态
我有 2 家商店,一家是关于房产的,一家是关于业主的。 每项财产都由一个或多个所有者拥有,我必须按所有者对财产进行分组。所以就像一个地图结构Map
如何检索您问的两个对象? Redux 选择器!
对于属性,我有一个带有选择器的外观,
properties$ = this.store$.select(PropertySummarySelectors.selectProperties);
这就是现在使用的,用于显示页面上所有属性的列表,现在我们必须添加一个选项卡,它将按所有者对属性进行分组。为什么财产没有关于所有者的数据,所有者是人,我们不能存储个人信息,隐私的东西。
在另一个所有者商店中,我们还有一个外观,其中有一个我需要的选择器
ownersOfProperty$ = (propertyId) => this.store$.select(OwnerSelectors.selectOwnerForProperty, propertyId);
这将使用以下选择器返回给定属性的所有者列表:
export const selectOwnerForProperty = createSelector(
selectOwnerForProperty,
(ownersPerProperty, propertyId) => ownersPerProperty[propertyId] || []
);
因此,在调用 OwnerFacade 时,我得到一个 Observable。
我的想法是,当调用 property$ 选择器时,对于该列表中的每个对象,调用另一个外观来获取所有者。
this.property$ = this.propertySummaryFacade.properties$.pipe(
map(properties => properties.map(
async property => {
property.owners = await this.ownerFacade.ownerOfProperty$(property.id);
return property;
})
)
);
它没有解决它,它看起来很丑陋。如何正确组合 2 个商店的这 2 个状态,而不在后端扩展我的 Property 对象,由于隐私法规,这也不容易。
I have 2 stores, one about properties and one about owners.
Each property is owned by one or more owners, and I have to group the properties by owner. So like a map structure Map<Owner, Property[]>.
How to I retrieve both objects you ask? Redux selectors!
For properties I have a facade which has a selector
properties$ = this.store$.select(PropertySummarySelectors.selectProperties);
This is what is used now, to show a list of all properties on a page, now we have to add a tab, which will group the properties by owner. Why doesn't property have data about the owner, the owner is human and we can't store personal information on our side, privacy stuff.
In another store for owners, we also have a facade, which has a selector that I need
ownersOfProperty$ = (propertyId) => this.store$.select(OwnerSelectors.selectOwnerForProperty, propertyId);
This will return a list of owners for the given property using the following selector:
export const selectOwnerForProperty = createSelector(
selectOwnerForProperty,
(ownersPerProperty, propertyId) => ownersPerProperty[propertyId] || []
);
So when calling the OwnerFacade, I get an Observable.
My idea was, when calling the property$ selector, for every object in that list, call the other facade to get the owners.
this.property$ = this.propertySummaryFacade.properties$.pipe(
map(properties => properties.map(
async property => {
property.owners = await this.ownerFacade.ownerOfProperty$(property.id);
return property;
})
)
);
It doesn't resolve it, it looks ugly. How to I combine these 2 states for 2 stores correctly, without extending my Property object on the backend side, which is again not easy due to privacy regulation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个组合两个实体的选择器。
使用该选择器作为“基本”选择器
在组件中使用
selectOne
选择器。Create a selector that combines both entities.
Use that selector as a "base" selector
Use the
selectOne
selector in components.