Skip to Content
0

SAPUI5 Publish and subscribe problem

Feb 14, 2017 at 07:37 AM

79

avatar image

Hi,

I am using publish/subscribe to pass data from master to detail view.

Master controller.js

var bins = { source: source, destination: destination };
var oEventBus = sap.ui.getCore().getEventBus();
oEventBus.publish("Master", "GetBatches", { bins });

Detail controller.js

onInit: function() {
    this.oInitialLoadFinishedDeferred = jQuery.Deferred();
    var oEventBus = sap.ui.getCore().getEventBus();
    oEventBus.subscribe("Master", "GetBatches", this.onGetBatches, this);

    this.getRouter().attachRouteMatched(this.onRouteMatched, this);		
	},
	onGetBatches : function (sChanel, sEvent, oData) {
//		if(sEvent === "GetBatches"){
			var sourceBin = oData.source;
			var destBin   = oData.destination;
//		} 	
	},

The problem is that when the detail controller gets into onInit, it does not call the function onGetBatches at all but instead it jumps to the onRouteMatched.

Does anyone have an idea, why onGetBatches is not called?

Thanks,

Tim

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Dominique Pierre Feb 14, 2017 at 09:56 AM
0

Hi,

Why do you want to use the event bus to pass parameters from one view to the other? Why don't you use a navigation parameter when you navigate from master to detail? The walkthrough shows how to do it: https://sapui5.hana.ondemand.com/#docs/guide/2366345a94f64ec1a80f9d9ce50a59ef.html

Cheers,

Pierre

Show 5 Share
10 |10000 characters needed characters left characters exceeded

Thanks for your comment, can you help with the navigation then?

My component.js

subroutes: [{
pattern: "detail",
name: "detail",
view: "Detail",
viewLevel: 2,
targetAggregation: "detailPages"
}]

Master.js

this.getRouter().navTo("detail", {
from : "main",
})

Parameters I want to pass are e.g. source and destination.

So I have tried in the Detail.js

var source = oEvent.getParameter("arguments").source;
var destination = oEvent.getParameter("arguments").destination;
0

Read the tutorial carefully. You have to specify the parameter in the pattern:

{
	"pattern": "detail/{invoicePath}",
	"name": "detail",
	"target": "detail"
}

And in the navTo:

oRouter.navTo("detail", {
	invoicePath: oItem.getBindingContext("invoice").getPath().substr(1)
});

Then you can get it from the event when the pattern is matched:

oEvent.getParameter("arguments").invoicePath;
0

Ok, how can I pass 2 parameters?

0

You can define multiple parameters in the pattern:

"product/{ProductId}/detail/{DetailId}"
1

Thank you!

0