What is the EventBus in SAPUI5 for?
Asked Answered
A

1

6

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

Atavism answered 12/5, 2016 at 13:1 Comment(0)
S
21

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 sap/ui/core/EventBus:

  • Globally: sap.ui.getCore().getEventBus(); (Deprecated since UI5 1.119) *
  • Component-based: this.getOwnerComponent().getEventBus(). It is recommended to get the EventBus from the component since it reduces the chance of accidentally reusing existing event names. Additionally, it ensures that your listeners are properly removed once the component gets destroyed, e.g. when the user navigates back to "Home" in Fiori launchpad.

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

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:

sapui5 event bus

Sample code

API reference: sap/ui/core/EventBus

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();
    // Broadcast the event:
    bus.publish("channelABC", "awesomeEvent", Object.freeze({
      // with myData ...
    }));
  },
}

* With UI5 1.119 and above, use sap/ui/core/EventBus.getInstance for global usage instead.

Stripteaser answered 12/9, 2017 at 23:32 Comment(7)
Can eventbus be used to share data and parameters between controllers? I'm currently using a blocks architecture and need to pass information from one controller to another but am unsure how to :/Impotence
@EldwinCheung Sure, as shown in the sample code above, the file 1 can be one controller and file 2 another. When publishing, simply pass the data as the third argument in an object. The passed data will be available in the third function parameter of the subscriber.Stripteaser
Ah, so the parametersMap in fnShouldDoSomething is the data that's received from the broadcast? Also, what is the subscriber in the context you mentioned?Impotence
@EldwinCheung Exactly. As mentioned in the API reference: > The channel is provided as first argument of the handler, and the event identifier is provided as the second argument. The parameter map carried by the event is provided as the third argument (if present). Handlers must not change the content of this map. By subscriber, I meant the handler, in our case, the shouldDoSomething function. Try and let us know if it worked!Stripteaser
were would you put the subscribe logic for event bus? in Component Controller, or in Base Controller, or in App.controller or in Main.controller? or further up in someController?Lumbering
@Lumbering I think it's not really relevant where exactly as long as the app can guarantee to subscribe earlier than the respective publish call to avoid missing the event notification. The answer is as always: depends on what the app is trying to achieve (and why) with the custom event.Stripteaser
But in my opinion, the use of EventBus is quite limited.. often when there is only 1 View "active" (1 Page), the other views can't do anything, therefore its questionable if you'll ever use eventbus to trigger some method in some other Controller (usually, they're connected to the view..). It might make sense to call it if there are nested views inside the main view like in those simple eventbus MessageToast examples, but I didn't see any more sophisticated use case example.. you could of course built a method archive inside 1 huge controller connected with an event listener.. but useful?Lumbering

© 2022 - 2024 — McMap. All rights reserved.