Skip to Content
0
May 03, 2018 at 03:12 PM

Updating an item counter for a Cart button

1127 Views Last edit May 03, 2018 at 03:16 PM 2 rev

Hi,

I am implementing a cart function into my UI5 app to collect a collection of items for further processing. My cart works fine as a feature in my Master-Detail app.

One thing I'd like to add is a counter next to the cart button to inform the user how many items currently are in the cart. My Gateway service happily returns the correct value when I use the $count option. So far, so good.

What I'm struggling with is the

  1. the correct location to update the counter next to the button
  2. retrieving the oData response

re 1: I decided to retrieve the counter within the onBeforeRendering method. Although I am happy to listen to any advice here.

re 2: I am using the code below.

onBeforeRendering: function() {
	var oCountModel = new sap.ui.model.odata.v2.ODataModel("<oData GW service>");

	oCountModel.read("/cartSet/$count", {
		method: "GET",
		async:false,
		success: function(data) {
			var response = (JSON.stringify(data));
				},
		error: function() {
				}
	});
},

The above code call the service synchronous, but doesn't return the service response ("data is undefined"). However, I can see the response later in the Network tab in Chrome.

So basically, the service works and is called, but for some reason I can't seem to get the resulting response.

Any ideas/hints where I am going wrong with this?

Attachments