所有經過http請求從Commerce Cloud背景到Spartacus前台渲染的資料,都會經曆下列這個generic步驟:

convertSource(source, injectionToken) {
return this.getConverters(injectionToken).reduce((target, converter) => {
return converter.convert(source, target);
}, undefined);
}
CMS page資料的normalize過程:
let OccCmsPageNormalizer = class OccCmsPageNormalizer {
convert(source, target = {}) {
this.normalizePageData(source, target);
this.normalizePageSlotData(source, target);
this.normalizePageComponentData(source, target);
this.normalizeComponentData(source, target);
return target;
}
第一步:NormalizePageData,給page打上load時間戳:
第二步:normalizePageSlotData
按照position給target設定鍵值對:
第三步: normalizePageComponentData
normalizePageComponentData(source, target) {
for (const slot of source.contentSlots.contentSlot) {
if (slot.components.component &&
Array.isArray(slot.components.component)) {
for (const component of slot.components.component) {
const comp = {
uid: component.uid,
typeCode: component.typeCode,
properties: component.properties,
};
if (component.typeCode === CMS_FLEX_COMPONENT_TYPE) {
comp.flexType = component.flexType;
}
else if (component.typeCode === JSP_INCLUDE_CMS_COMPONENT_TYPE) {
comp.flexType = component.uid;
}
else {
comp.flexType = component.typeCode;
}
target.page.slots[slot.position].components.push(comp);
}
}
}
}
第四步normalizeComponentData執行完畢之後,target結構包含的就是前端Spartacus處理起來比較友善的資料結構:
每個slot有slot id,每個slot id對應Components數組,每個Component由uid,typeCode和flexType唯一辨別:
Component數組裡有Component的詳細資料: