SAP 电商云 Spartacus UI Event Service 实现明细介绍

简介: Spartacus 事件服务提供了一个事件流,开发人员可以在不与特定组件或模块紧密集成的情况下使用它。

Spartacus 事件服务提供了一个事件流,开发人员可以在不与特定组件或模块紧密集成的情况下使用它。在Spartacus中,事件系统用于构建与第三方系统的集成,如标签管理器(Tag Management)和网络跟踪器。

event.service.ts 文件中导入 EventService

image.png

事件服务还允许开发人员解耦某些组件。例如,可能有一个分派事件的组件,即事件的产生者,和另一个响应此事件的组件,即事件的监听者。

二者之间不需要有任何硬依赖关系。

事件是由TypeScript类驱动的,TypeScript类是给定事件的签名,可以实例化。示例如下:

import { CxEvent } from "@spartacus/core";
export class CartAddEntryEvent extends CxEvent {
  cartId: string;
  userId: string;
  productCode: string;
  quantity: number;
}

下图是另一个 LanguageSetEvent,继承自 CxEvent,在此基础上增添了 activeLanguage 属性:


34.png

如何监听一个事件?

要观察给定类型的事件,可以获取事件类型的流,然后在需要的时候订阅它。下面是一个 CartAddEntryEvent 监听的例子:

constructor(events: EventService){}
/* ... */
const result$ = this.events.get(CartAddEntrySuccessEvent);
result$.subscribe((event) => console.log(event));

如果需要比特定事件中包含的数据更多的数据,您可以将该数据与其他流(stream)组合。例如,可以从 Spartacus facade 收集额外的数据。


下面是一个响应“添加到购物车事件”的例子,然后等待购物车处于 stable 状态(因为 OCC购物车需要从后端重新加载),然后将所有的购物车数据附加到事件中的数据:

constructor(
    events: EventService,
    cartService: ActiveCartService
    ){}
/* ... */
const result$ = this.events.get(CartAddEntrySuccessEvent).pipe(
    // When the above event is captured, wait for the cart to be stable
    // (because OCC reloads the cart after any cart operation)...
    switchMap((event) =>
        this.cartService.isStable().pipe(filter(Boolean), mapTo(event))
    ),
    // Merge the state snapshot of the cart with the data from the event:
    withLatestFrom(this.cartService.getActive()),
    map(([event, cart]) => ({ ...event, cart }))
);

上述代码的关键点:

  • switchMap 需要返回一个 Observable 对象。
  • 通过 filter 保证 Cart 处于 Stable 时再进行后续处理。
  • mapTo event 丢弃了 Cart Stable 状态,重新映射回 event 对象
  • withLatestFrom:从 cartService 读取 Stable 的 Cart 数据,此时通过前面的 filter 操作,能保证 Cart 一定已经 Stable 了。
目录
相关文章
|
2月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
32 0
|
2月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
17 0
|
2月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍
33 0
|
2月前
|
JSON 前端开发 测试技术
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
24 0
|
2月前
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
22 0
|
1月前
什么是 SAP ABAP 里的 Subscreen
什么是 SAP ABAP 里的 Subscreen
16 1
什么是 SAP ABAP 里的 Subscreen
|
2月前
|
开发者 UED
关于 SAP UI5 sap.m.Column 的 demandPopin 属性
关于 SAP UI5 sap.m.Column 的 demandPopin 属性
15 0
|
3月前
|
JSON 数据格式
SAP UI5 Class sap.ui.model.Context 的作用介绍
SAP UI5 Class sap.ui.model.Context 的作用介绍
32 0
|
3月前
|
机器学习/深度学习 人工智能 监控
SAP Sales Cloud,Service Cloud 和 SAP BTP 平台上的 AI 集成场景
SAP Sales Cloud,Service Cloud 和 SAP BTP 平台上的 AI 集成场景
70 0
|
4月前
|
存储 缓存 前端开发
关于 SAP Spartacus Optimization Engine 里的 cache 参数使用注意事项
关于 SAP Spartacus Optimization Engine 里的 cache 参数使用注意事项
20 0