data-binding - SAPUI5:在 Controller 中检索模型对象

标签 data-binding sapui5

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

在细节 Controller 中,我通过以下方式将模型绑定(bind)到 View (此方法附加到路由器对象)。

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

如何从此 Controller 中访问实际绑定(bind)的模型对象?

我得到的最接近(但似乎有点太复杂)如下
var path = this.getView().getElementBinding('contracts').sPath.substring(1);
var model = this.getView().getModel('contracts').oData[path];

最佳答案

好吧,您的方法并不遥远,并且确实与hirses几乎相同。

关键是绑定(bind)不包含“仅”绑定(bind)的模型对象。它包含有关模型、“绑定(bind)对象”的路径和上下文的信息。这些可以从绑定(bind)中检索。要访问“绑定(bind)对象”,您基本上有两个可用的路径。

从绑定(bind)中获取模型和路径并通过模型访问“绑定(bind)对象”:(这是你和hirse概述的)

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

或者获取上下文和路径并以这种方式访问​​“绑定(bind)对象”:
var context = this.getView().getElementBinding('contracts').oContext;
var boundObject = context.getProperty(context.getPath());

在没有对此做太多研究的情况下,我更喜欢第二种选择。它似乎更符合上下文绑定(bind)的意图。

关于data-binding - SAPUI5:在 Controller 中检索模型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32114263/

相关文章:

c# - AvalonEdit 中的双向绑定(bind)不起作用

javascript - Polymer v1.3.1 databind 无法使用或不使用带有重复模板和 json 数组的 iron-ajax

angularjs - 在 Angular 中,来自范围变量 *before* ngModel 的绑定(bind)属性绑定(bind)输入的 `value`

java - 多个数据绑定(bind)错误

sapui5 - sapui5 和 openui5 的应用程序

wpf - Caliburn.Micro : How to bind a specific Item of Conductor. Collection.AllActive 到 ContentControl

javascript - SAPUI5 - 存在异步函数时的路由问题

SAPUI5:如何过滤具有 2 个或更多值的数据

javascript - 一个应用程序的自定义 CSS 反射(reflect)在启动板内的每个应用程序中

testing - 使用 OPA 在 SAPUI5 中进行负测试