SAPUI5:在控制器中检索模型对象

Mar*_*arc 5 data-binding sapui5

我有一个使用OData服务(在manifest.json中声明)的主从应用程序。

在详细信息控制器中,我通过以下方式将模型绑定到视图(此方法已附加到路由器对象)。

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

如何从此控制器中访问实际的绑定模型对象?

我最近的(但似乎有点太复杂了)如下

var path = this.getView().getElementBinding('contracts').sPath.substring(1);
var model = this.getView().getModel('contracts').oData[path];
Run Code Online (Sandbox Code Playgroud)

Car*_*ten 6

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);
Run Code Online (Sandbox Code Playgroud)

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());
Run Code Online (Sandbox Code Playgroud)

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.