SAP Commerce Cloud Spartacus UI 的 ActionClass 数据结构设计
以 CmsSetPageSuccessIndex 為例:
運行時該 Action 類包含三大字段:
(1) meta
(2) payload
(3) type
meta
meta 包含的 loader 字段和 meta 本身都是一個 object.
meta.entityId 和 entityType 是該 action payload 的標識位。
loader 表明該 entity 加載狀況。
payload
包含的業務數據:
type
字符串類型。
從 上圖 Prototype 能看出,CmsSetPageSuccessIndex 的原型鏈類為 EntitySuccessAction.
這一點也能從其實現代碼看出:
export class CmsSetPageSuccessIndex extends StateUtils.EntitySuccessAction {readonly type = CMS_SET_PAGE_SUCCESS_INDEX;constructor(pageContext: PageContext, payload: Page) {super(pageContext.type, pageContext.id, payload);} }查看 EntitySuccessAction 的實現:
屬性1,meta 屬性,定義在該類里。
屬性2,payload,通過構造函數參數定義。
屬性3,type,定義在上圖81行,然后被 CmsSetPageSuccessIndex 重載。
EntitySuccessAction 構造函數接收三個參數:entityType,id 和 payload.
通過 entityType 和 id,生產出 meta 字段。
entitySuccessMeta 可以看成一個構造器或者工廠函數:
根據 entityType 和 id,生產出 EntityLoaderMeta, 后者是 EntityMeta 和 LoaderMeta 的聯合。
export interface EntityMeta {entityType: string;entityId: string | string[];entityRemove?: boolean; }export interface LoaderMeta {entityType: string;loader: {load?: boolean;error?: any;success?: boolean;}; }采用三個點的語法,是因為這兩個 interface,都有同名字段:entityType
LoaderMeta 的運行時數據,通過構造器 loadMeta 制造:
export function loadMeta(entityType: string): LoaderMeta {return {entityType: entityType,loader: {load: true,},}; }EntityMeta 字段的值,通過 entityMeta 構造:
export function entityMeta(type: string, id: string | string[]): EntityMeta {return {entityType: type,entityId: id,}; }運行時通過 PageEffect 從 Commerce Cloud 后臺讀取 CMS 數據成功后,新建 ActionClass,將負載通過構造函數參數傳入。
更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的SAP Commerce Cloud Spartacus UI 的 ActionClass 数据结构设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果供应商 Cirrus Logic 暗
- 下一篇: 明日之后步枪兵怎么玩