SAPUI5-事件总线

SAPUI5-事件总线,sapui5,Sapui5,有人能帮我举个例子吗 我试图将数据从一个视图传递到另一个视图,我在另一篇文章中看到,实现这一点的最佳实践是使用EventBus,我找到了一些示例,但对我来说并不适用 这是我的密码: 家庭控制器: 这里有文档,单击其中一个后,我想导航到通过该对象的其他视图 onInit: function(){ var eventBus = sap.ui.getCore().getEventBus(); eventBus.publish("Home", "HomeEvent", { text :

有人能帮我举个例子吗

我试图将数据从一个视图传递到另一个视图,我在另一篇文章中看到,实现这一点的最佳实践是使用EventBus,我找到了一些示例,但对我来说并不适用

这是我的密码:

家庭控制器:

这里有文档,单击其中一个后,我想导航到通过该对象的其他视图

onInit: function(){
    var eventBus = sap.ui.getCore().getEventBus();
    eventBus.publish("Home", "HomeEvent", { text : "Message From Home"});
},
onPressButton: function(oEvent){
    this.getRouter().navTo("view2");
}
VIEW2控制器

在这里,我想订阅EventBus来获取我之前传递的对象,以获取数据对象

onInit: function(){

},
onPressButton: function(channel, event, data){
    var eventBus = sap.ui.getCore().getEventBus();
    eventBus.subscribe("Home", "HomeEvent", this.showMessage, this);
},
showMessage: function(sChannel, sEvent, oData){
    var msg = oData.text;
    MessageToast.show(msg);
},
onNavBack: function () {
    return BaseController.prototype.onNavBack();
}

谢谢

主视图:

// if not defined on Base Controller..
getEventBus : function() {
    return sap.ui.getCore().getEventBus();
},

//Time of subscription
onInit : function() {
     var oEventBus = this.getEventBus();
     oEventBus.subscribe("DetailView", "Binded", this.onDetailBinded, this);
}

 //Method if eventbus registers a "publish" command
onDetailBinded : function(){
      // do your thing in MasterView with event bus executed from DetailView
},
getEventBus : function() {
     return sap.ui.getCore().getEventBus();
},
             
//Publish event
// e.g. on Button Press or whatever action your want to trigger
YourCallingMethod : function() { 
      var oEventBus = this.getEventBus();
      oEventBus.publish("DetailView", "Binded");
}
详细视图:

// if not defined on Base Controller..
getEventBus : function() {
    return sap.ui.getCore().getEventBus();
},

//Time of subscription
onInit : function() {
     var oEventBus = this.getEventBus();
     oEventBus.subscribe("DetailView", "Binded", this.onDetailBinded, this);
}

 //Method if eventbus registers a "publish" command
onDetailBinded : function(){
      // do your thing in MasterView with event bus executed from DetailView
},
getEventBus : function() {
     return sap.ui.getCore().getEventBus();
},
             
//Publish event
// e.g. on Button Press or whatever action your want to trigger
YourCallingMethod : function() { 
      var oEventBus = this.getEventBus();
      oEventBus.publish("DetailView", "Binded");
}

假设您有两个视图v1和v2

在导航到v2之前,需要在v2中调用函数

然后在v1中编写一个事件总线发布事件

在任何情况下,如果您希望在导航之前调用v2中的某些函数,请编写下面的代码

var a = { context: *YourJSONData*, viewName:'xyz'}; //here context and view name are passed as params in the function

sap.ui.getCore().getEventBus().publish("app", "**uniquename**", a);
然后在v2上编写订阅事件

onInit : function () {
        // subscribe to refresh detail tab
        var bus = sap.ui.getCore().getEventBus();
        bus.subscribe("app", "**uniquename**", this.**YourFunctionName**, this);        
    },

**YourFunctionName**:function(channelId, eventId, data) {{
        //Write you code in function as needed data will have whatever you passed
    },

您只需要传递一个ID吗?通常在这种情况下,您不需要事件总线。相反,您可以在路由中传递参数。事件总线在UI5中没有像过去那样得到很好的使用。如果你读到了2015年、2016年的文章,不要以为这仍然是真的。大多数数据都可以在modelsbtw的帮助下传递。master和detail可以用作View1和View2的同义词。。。