Search code examples
sapui5

SAPUI5 get current context in detail view


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?


Solution

  • 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.