6

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?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Anonymoose
  • 2,389
  • 6
  • 36
  • 69

3 Answers3

20

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.

Tim Gerlach
  • 3,390
  • 3
  • 20
  • 39
  • I use your bindingContext/path/object calls and add `console.log(object);` after that. In Chromes console I can see the content of the object now, with all the attributes it has. But when I try to access a property like this: `console.log(object.Id);` I get `undefined`. Any idea why that is happening? – Michael K. Apr 23 '15 at 09:02
  • @Deftoned: Sometimes I face this issue when debugging objects from the model. This might occur since you´re holding a pointer to the object and not an object copy. You can try to stop your program immediately after retreiving the object from the bindingContext and read it rather then just logging it to the console. – Tim Gerlach Apr 23 '15 at 11:51
0

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

evt.getSource().getSelectedItem().getBindingContext("yourModelName").getObject();
Aurel Havetta
  • 455
  • 4
  • 9
  • Actually event 'Select' is deprecated, instead use 'selectionChange' event. Use the below code for eventhandler var oSelectedItem = oEvent.getParameter("listItem"); oSelectedItem.getBindingContext("yourModelName").getObject(); – Aurel Havetta Jul 26 '17 at 14:46
  • And "listItem" works only for single selection. For multiple selections use "listItems" which returns array of selected items – Aurel Havetta Jul 26 '17 at 14:47
-1

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

 evt.getParameter("listItem").getBindingContextPath();
Alberto
  • 1,423
  • 18
  • 32