6
votes

I have a master-detail application that consumes an OData service (declared in manifest.json).

In the detail controller, I bind the model to the view in the following way (this method is attached to a router object).

_onObjectMatched: function(oEvent) {
    this.getView().bindElement({
        path: "/ContractCompSet('" + oEvent.getParameter("arguments").id + "')",
        model: "contracts"
    });
}

How can I access the actual bound model object from within this controller?

Closest I got (but seems too be a little too complicated) is as follows

var path = this.getView().getElementBinding('contracts').sPath.substring(1);
var model = this.getView().getModel('contracts').oData[path];
2
Hi Marc, it's been some time since you posted it but I have the same question: I would like to have access to the single data in the Detail Controller and so far I can only make use of the data in the view itself. Did you solve above problem?Skye
Yes, you can access all data of your view in your controller. I usually do this.getView().getBindingContext("NAME_OF_MODEL"). This gives me the context which is bound to the view. You can then do .getObject() to access the actual data. Give me a code example and I can try to help.Marc

2 Answers

6
votes

Well your approach isn't to far off and is indeed pretty much the same as hirses.

The point is the binding does not contain "just" the bound model object. It contains the information about model, path to the "bound object" and context. These can be retrieved from the binding. To access the "bound object" you then have basically two paths available.

Get the model and path from the binding and access the "bound object" via the model: (this is what you and hirse outlined)

var path = this.getView().getElementBinding('contracts').sPath;
var boundObject = this.getView().getModel('contracts').getProperty(path);

Or get the context and path and access the "bound object" that way:

var context = this.getView().getElementBinding('contracts').oContext;
var boundObject = context.getProperty(context.getPath());

Without having done to much research into this I would prefer the second option. It just seems more along the line of how the context binding is intended.

2
votes

I would have thought,

this.getView().getModel('contracts')

gives you the Model Object (as in an object of type sap.ui.model.Model or subclass).

If you are referring to the data in the Model, you can use the following:

this.getView().getModel('contracts').getProperty("/")