如何在SAPUI5中使用getEventBus方法 [英] How to use getEventBus method in SAPUI5

查看:252
本文介绍了如何在SAPUI5中使用getEventBus方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力了解 getEventBus()。有人可以提供一个教程或者最好的例子,我们如何实现 getEventBus()

解决方案

我提供了一个例子来回答另一个问题这里



简而言之,您可以调用 sap.ui.getCore()。getEventBus()来访问EventBus实例。由于它来自核心,所有的观点/控制器都是一样的。 EventBus为您提供发布/订阅功能。例如,您可以在控制器A中发布事件并通知订阅的控制器B.一个简单的例子主要来自我的其他答案:



订阅EventBus :

  var eventBus = sap.ui.getCore()。getEventBus(); 
eventBus.subscribe(channel1,event1,this.handleEvent1,this);

当然,您可以根据需要为您的频道和活动命名。第三个参数表示功能,在发布事件的情况下将被调用。最后一个参数是范围,'this'将指向给定的函数。



您的 handleEvent1 函数可以看起来像这样:

  handleEvent1:function(channel,event,data){
var customData = data.customData
}

将事件发布到EventBus:

  var customData = {} //任何你最终想要传递
var eventBus = sap.ui.getCore()。getEventBus );

eventBus.publish(channel1,event1,
{
customData:customData
}
);

如果你有更多的问题,让我知道,所以我会扩展它。

I am trying to understand getEventBus(). Can somebody provide a tutorial or best example where and how we can implement getEventBus().

解决方案

I´ve provided an example to answer another question here.

To put it in a nutshell, you can call sap.ui.getCore().getEventBus() to get access to the EventBus instance. As it comes from the core it´s the same across all of your views/controllers. The EventBus provides you with the publish/subscribe functionality. This for example enables you to publish an event in Controller A and notify the subscribed Controller B. A simple example largely from my other answer:

Subscribing to the EventBus:

var eventBus = sap.ui.getCore().getEventBus();
eventBus.subscribe("channel1", "event1", this.handleEvent1, this);

Of course you can name your channel and events as you wish. The third parameter indicates the function, that will be called in case of published events. The last paramter is the scope, 'this' will point to in the given function.

Your handleEvent1 function could look like this:

handleEvent1 : function(channel, event, data) {
    var customData = data.customData
}

Publishing events to the EventBus:

var customData = {}  // anything you eventually want to pass
var eventBus = sap.ui.getCore().getEventBus();

eventBus.publish("channel1", "event1", 
    {
        customData: customData
    }
);

If you have more questions about it let me know so I´ll extend it.

这篇关于如何在SAPUI5中使用getEventBus方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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