SAP UI5中的格式化程序返回视图实例,而不是控件实例

时间:2019-01-21 12:39:39

标签: sapui5

我在Formatter文件中使用SAP UI5 1.52,该文件是一个单独的文件,已加载到控制器中。但是格式化程序方法中的this返回视图实例,而不是控件实例。

在调用格式化程序时,它仅采用相对路径,并在给出绝对路径时抛出错误。

请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

formatter.js

sap.ui.define([], function() { // location: "path/to/my/formatter.js"
  "use strict";

  return { // plain object
    getFormatted: function() {
      // Here, this === control instance
    }
  }
}, /*export*/true); // <-- Enables accessing this module via global name "path.to.my.formatter"

控制器

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "path/to/my/formatter" // Just to trigger defining the formatter module
], function(Controller) {
  // ...
  return Controller.extend("...", {
    // formatter: formatter <-- remove it!
  });
});

查看

<MyControl property="{
  path: '...',
  formatter: 'path.to.my.formatter.getFormatted'
}" />

答案 1 :(得分:0)

在控件的属性中,您通常编写 formatter:'.formatter.functionName'

只需将其更改为:formatter:'namespace.controllerFolder.controllerName.prototype.formatter.functionName'

并且 this 现在将引用控件实例而不是您的控制器。

简单易行:)