SAP Spartacus ProductConnector和ProductService实现

时间:2022-07-25
本文章向大家介绍SAP Spartacus ProductConnector和ProductService实现,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

名称:ProductConnector 路径:在core文件夹下

通过ProductAdapter向Commerce Cloud发起数据请求:

逻辑很简单,直接调用adapter的load方法。

什么时候ProductConnector会被调用?

let ProductEffects = class ProductEffects {
    constructor(actions$, productConnector) {
        this.actions$ = actions$;
        this.productConnector = productConnector;
        // we want to cancel all ongoing requests when currency or language changes,
        this.contextChange$ = this.actions$.pipe(ofType(CURRENCY_CHANGE, LANGUAGE_CHANGE));
        this.loadProduct$ = createEffect(() => ({ scheduler, debounce = 0 } = {}) => this.actions$.pipe(ofType(LOAD_PRODUCT), map((action) => ({
            code: action.payload,
            scope: action.meta.scope,
        })), 
        // we are grouping all load actions that happens at the same time
        // to optimize loading and pass them all to productConnector.getMany
        bufferDebounceTime(debounce, scheduler), mergeMap((products) => merge(...this.productConnector
            .getMany(products)
            .map(this.productLoadEffect))), withdrawOn(this.contextChange$)));
    }

有17个product需要加载:

loadProduct$的实现,包含了加载多个product的逻辑:

这17个product是外层传进来的:

看这个getProductForScope是何时被调用的:

又出现了一个ProductService:

位于facade层:

Spartacus-storefront.js:

这个ProductCarouselComponent位于storefrontlib文件夹内: