cancel
Showing results for 
Search instead for 
Did you mean: 

getBindingContext() always returns undefined

trmt
Explorer
0 Kudos

Hi!

On my journey to learning SAP UI5 I stumbled upon these resources SAP uploaded on their Youtube channel and I gave the tutorial a try to learn some new techniques.

In the Routing & Navigation episode, I encountered an issue.

I've set everything to get the data from an external source, and created the model in the manifest.json file:

"sap.ui5":{
   ...
   "models":{
      ...
      "myModel":{
         "dataSource":"myModelAPI"
      }
   }
}

Then I can access and view this data in my XML view: *this is working

<mvc:View
	controllerName="myApp.controller.myController"
	xmlns="sap.m"
	xmlns:mvc="sap.ui.core.mvc"
	xmlns:core="sap.ui.core">
	<Table id="myTable"
	       items="{myModel>/}"
               itemPress=".onItemPressed"
	       fixedLayout="false">
		<columns>
                    ...
		</columns>
		<items>
			<ColumnListItem type="Active">
				<cells>
					<Text text="{myModel>id}"/>
					<Text text="{myModel>name}"/>
					...
				</cells>
			</ColumnListItem>
		</items>
	</Table>
</mvc:View><br>

Now, in my controller when I try to access the item clicked and send the id of the clicked item with the navTo() method and onto the next page, I get undefined from my getBindingContext() method even if I provide the name of my model in it:

sap.ui.define(
    ["myApp/controller/BaseController"],
    function(
        BaseController
    ) {
        "use strict"

        return BaseController.extend(
            "myApp.controller.myController", {
                onInit: function() {
                    let oRouter = this.getRouter()
                    oRouter
                        .getRoute("myRoute")
                        .attachMatched(this._onRouteMatched, this)
                },

                onItemPressed: function(oEvent) {
                    let oItem = oEvent.getSource()
                    let oCtx = oItem.getBindingContext("myModel") 
                    console.log(oCtx) // always undefined
                }
            }
        )
    }
)<br>

What is wrong with above or what am I doing wrong? Hopefully I've provided enough context so you can tell.

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

Wojciech_P
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

can you check what object is the returned from oEvent.getSource(). If it is a table, please use following code:

let oItem =  oEvent.getParameter("listItem");
let oCtx = oItem.getBindingContext("myModel")

Best Regards

trmt
Explorer
0 Kudos

That was it.

So essentially if I would've had a List then I could've gotten oItem with oEvent.getSource() otherwise as I have a Table I can get it with oEvent.getParameter("listItem")? Can you please guide me to the right documentation where this is explained in greater detail? Thank you!

Fjaoos
Participant

Hi,

the sap.m.Table (coming from sap.m.ListBase) does have the event itemPress which you created a handler for. This means that the Table does fire the event and is thus the source.

You can get the item from the listItem parameter of the event.

However, you can also add a handler for the press event of any listitem: https://sapui5.hana.ondemand.com/#/api/sap.m.ListItemBase%23events/press

In this handler the call of oEvent.getSource() would give you the listitem.

Regards

Nils

trmt
Explorer
0 Kudos

Cool, thank you!

I understood now, I've managed to do it both ways and it's working

Answers (1)

Answers (1)

radoslaw_kiela2
Participant
0 Kudos