6
votes

I have a SAPUI5 split-app with a master- and detail-view.

When I select an item in the side bar, I pass the context to the detail view, lets say product 1

onSelectProduct: function(evt){
    sap.ui.getCore().getEventBus().publish("app", "refreshProductDetail", {context : evt.getSource( ).getBindingContext()});   

},

this triggers following function which binds the context:

refresh: function(channelId, eventId, data){
    if (data && data.context) {
        this.getView().setBindingContext(data.context);
    }
},

Now when I perform an action like save, I want to get the current data of product 1 that is in the model.

However, when I use

this.getView().getBindingContext().getModel()

it returns the model with all the products. How do I know which one is currently being viewed by the user?

3

3 Answers

19
votes

You can use getPath() of a bindingContext to see what object is currently displayed:

this.getView().getBindingContext().getPath();

You could do something like this:

var bindingContext = this.getView().getBindingContext();
var path = bindingContext.getPath();
var object = bindingContext.getModel().getProperty(path);

If you only want a certain property of your displayed object you can do something like this:

var property = bindingContext.getProperty("<nameOfProperty>");

which returns a property of an object at specific context.

Update:

You can simply call getObject() of a bindingContext which returns the object in the model the current context points to:

var object = bindingContext.getObject();

See the documentation of Context for more information.

0
votes

For list item "select" event, to get currect selected item record from binding context use:.

evt.getSource().getSelectedItem().getBindingContext("yourModelName").getObject();
-1
votes

You can also use the parameter listItem to retrive the BindingPath from the event:

 evt.getParameter("listItem").getBindingContextPath();