这个例子有什么帮助我吗?
我正在尝试将数据从一个视图传递到另一个视图,我在另一篇文章中看到,使用EventBus的最佳做法是我发现了一些示例,但不适用于我。
这是我的代码:
HomeController:
这里有文档,一旦单击其中一个,我想导航到传递该对象的其他视图。
onInit: function(){
var eventBus = sap.ui.getCore().getEventBus();
eventBus.publish("Home", "HomeEvent", { text : "Message From Home"});
},
onPressButton: function(oEvent){
this.getRouter().navTo("view2");
}
View2Controller
在这里我想订阅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();
}
谢谢!
答案 0 :(得分:0)
考虑您有两个视图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
},
答案 1 :(得分:-1)
主视图:
// 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");
}