SAP 电商云 Spartacus UI Store 相关的设计明细

简介: SAP 电商云 Spartacus UI Store 相关的设计明细

Store

state.ts

定义了和 Site Context 业务相关的 State 数据模型。


定义数据模型的套路是:


export const SITE_CONTEXT_FEATURE = 'siteContext';
export interface StateWithSiteContext {
  [SITE_CONTEXT_FEATURE]: SiteContextState;
}

这是最顶层的 State 模型了。


SiteContextState 包含了三个子 State:

export interface SiteContextState {
  languages: LanguagesState;
  currencies: CurrenciesState;
  baseSite: BaseSiteState;
}

以 CurrenciesState 为例,不仅包含了 Entities 列表,还包含了当前 Active 状态的 Currency:

export interface CurrenciesState {
  entities: CurrencyEntities;
  activeCurrency: string;
}

再定义 Entities 列表:


export interface CurrencyEntities {
  [isocode: string]: Currency;
}

以上就是 Site Context 领域所需的 State 数据结构。


注意 SITE_CONTEXT_FEATURE 的使用场合,除了本文件定义 feature state 之外,还用在下列两个文件内:


e28bd5aa002e1aaa21ac8a11df8bd10b.png


场景1:用来创建 feature selector:


04ebdfccc1c00278b50f651335587438.png

场景2:使用 StoreModule.forFeature 注册 store:


26c331d41d2fc70227a951225f24d325.png


当使用 createSelector 和 createFeatureSelector 函数时,@ngrx/store 会跟踪调用选择器函数的最新参数。 因为选择器是纯函数,所以当参数匹配时可以返回最后一个结果,而无需重新调用选择器函数。 这可以提供性能优势,特别是对于执行昂贵计算的选择器。这种做法称为 memoization.


createFeatureSelector 是返回顶级(Top Level)的 Feature State的便捷方法。 它为状态的特征切片(Feature Slice)返回一个类型化(typed)的选择器函数。


注意 createFeatureSelector 的调用有两种写法。


写法1


下图 2 必须是 1 的一个切片,并且 3 的类型必须和 2 的类型一致:

7c3942dbca9e16dddb8348cb553b8028.png

2 的位置其实就是 result 的位置:


710e817accf13db4a4eddbc112bdf92a.png


写法2


import { createSelector, createFeatureSelector } from '@ngrx/store';
export const featureKey = 'feature';
export interface FeatureState {
  counter: number;
}
export interface AppState {
  feature: FeatureState;
}
export const selectFeature = createFeatureSelector<AppState, FeatureState>(featureKey);
export const selectFeatureCount = createSelector(
  selectFeature,
  (state: FeatureState) => state.counter
);

我做过测试,在 SAP 电商云 Spartacus UI 项目里,两种写法完全等价:


710e817accf13db4a4eddbc112bdf92a.png

可以顺利通过编译:

0a734814db61b543458f0c5895b29582.png

目录
相关文章
|
1月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
27 0
|
1月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
13 0
|
1月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍
28 0
|
1月前
|
JSON 前端开发 测试技术
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
21 0
|
1月前
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
20 0
|
16天前
什么是 SAP ABAP 里的 Subscreen
什么是 SAP ABAP 里的 Subscreen
13 1
什么是 SAP ABAP 里的 Subscreen
|
16天前
|
网络架构 开发者 UED
Spartacus 2211 的 provideOutlet 方法扩展 UI
Spartacus 2211 的 provideOutlet 方法扩展 UI
8 0
Spartacus 2211 的 provideOutlet 方法扩展 UI
|
1月前
|
开发者 UED
关于 SAP UI5 sap.m.Column 的 demandPopin 属性
关于 SAP UI5 sap.m.Column 的 demandPopin 属性
15 0
|
2月前
|
JSON 数据格式
SAP UI5 Class sap.ui.model.Context 的作用介绍
SAP UI5 Class sap.ui.model.Context 的作用介绍
29 0
|
3月前
|
JSON 开发者 数据格式
关于 SAP Spartacus LandingPage2Template 区域的 layout 设计实现
关于 SAP Spartacus LandingPage2Template 区域的 layout 设计实现
24 0

热门文章

最新文章