SAPUI5 中的 EventBus 是做什么用的? [英] What is the EventBus in SAPUI5 for?

查看:31
本文介绍了SAPUI5 中的 EventBus 是做什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下我们将使用什么以及何时使用

示例代码

订阅

{//控制器 AonInit:函数(){const bus = this.getOwnerComponent().getEventBus();bus.subscribe(channelABC",awesomeEvent",this.shouldDoSomething,this);},shouldDoSomething: function(channelId, eventId, parametersMap) {//在例如时得到通知做某事"来自控制器 B 的调用.},}

发布

{//控制器 B做某事:功能(我的数据){const bus = this.getOwnerComponent().getEventBus();bus.publish("channelABC", "awesomeEvent", { myData });//广播事件},}

API参考:sap/ui/core/事件总线

Can anybody explain for what and when we are going to use EventBus methods? Also what kind the activities of the same.

解决方案

EventBus in UI5 is a tool with which we can leverage publish-subscribe pattern in our app.

How do we get EventBus?

Currently, there are two APIs which return their own instance of EventBus:

  • Globally: sap.ui.getCore().getEventBus(); for

    • Standalone apps.
    • Component apps and their container app where developers have control over both.
  • Component-based: this.getOwnerComponent().getEventBus(); // this == controller. Especially for apps targeting Fiori Launchpad (FLP) where SAP explicitly warns not to get the EventBus from the core but from the component:

    If you need an event bus, use the event bus of the component. By this, you avoid conflicting event names and make sure that your listeners are automatically removed when the component is unloaded. Do not use the global event bus.

Note

  1. FLP destroys the component every time when the user navigates back Home.

  2. Make sure to have the module sap/ui/core/EventBus required before calling getEventBus() to properly declare the dependency and to avoid possible sync XHR when requiring it.

    sap.ui.define([ // or sap.ui.require
      // ...,
      "sap/ui/core/EventBus",
    ], function(/*...,*/ EventBus) { /*...*/ });
    

What is it for?

With EventBus, we can fire (via publish()), and listen (via subscribe()) to our own custom events freely:

  • Without having to use or extend any Control / ManagedObject / EventProvider classes,
  • Without knowing the existence of other involved listeners (if any),
  • Without accessing the object that fires the event (publisher). E.g.: No need to call thatManagedObj.attach*().

Publishers and subscribers stay ignorant to each other which makes loose coupling possible.

Analogous to the real world, EventBus is like a radio station. Once it starts to broadcast about all sorts of things on various channels, those, who are interested, can listen to a particular channel, get notified about a certain event, and do something productive with the given data. Here is an image that illustrates the basic behavior of an EventBus:

Sample code

Subscribe

{ // Controller A
  onInit: function() {
    const bus = this.getOwnerComponent().getEventBus();
    bus.subscribe("channelABC", "awesomeEvent", this.shouldDoSomething, this);
  },
  
  shouldDoSomething: function(channelId, eventId, parametersMap) {
    // Get notified when e.g. "doSomething" from Controller B is called.
  },
}

Publish

{ // Controller B
  doSomething: function(myData) {
    const bus = this.getOwnerComponent().getEventBus();
    bus.publish("channelABC", "awesomeEvent", { myData }); // broadcast the event
  },
}

See API reference: sap/ui/core/EventBus

这篇关于SAPUI5 中的 EventBus 是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆