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