合并 2 个 redux 存储的状态

发布于 2025-01-16 22:12:35 字数 1268 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素手挽清风 2025-01-23 22:12:35

创建一个组合两个实体的选择器。

const selectCombine = createSelector(
  selectEntities1,
  selectEntitities2,
  (ent1, ent2) => /* combine both into a single object */
)

使用该选择器作为“基本”选择器

const selectOne = (id) => createSelector(selectCombine, combine => combine[id])

在组件中使用 selectOne 选择器。

Create a selector that combines both entities.

const selectCombine = createSelector(
  selectEntities1,
  selectEntitities2,
  (ent1, ent2) => /* combine both into a single object */
)

Use that selector as a "base" selector

const selectOne = (id) => createSelector(selectCombine, combine => combine[id])

Use the selectOne selector in components.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文