How to have control instance as “this” in formatter?

99封情书 提交于 2019-12-13 22:07:36

问题


I am using SAP UI5 1.52. My formatter file is a separate file and loaded in the controller. But logging this in the formatter returns view instance rather than control instance.

I have referred this question before and tried using absolute path and changed the way object is returned in formatter. It throws an error saying function not found.


回答1:


UI5 1.69+

View

<MyControl xmlns:core="sap.ui.core" core:require="{ format: 'demo/model/format' }"
  property="{
    path: '...',
    formatter: 'format'
  }"
/>

Formatter

sap.ui.define([], function() { // location: "demo/model/format.js"
  "use strict";

  return function(data) {
    // this === control instance
  };
});

As of UI5 1.69, we can require modules directly in the view definition. Requiring and assigning the formatter directly in the binding info lets us to use this as the corresponding control instance.

  • No need to require the formatter module in controller beforehand.
  • No need to export the formatter under a global name.

UI5 1.68 and below

View

<MyControl property="{
  path: '...',
  formatter: 'demo.model.format'
}" />

Formatter

sap.ui.define([], function() { // location: "demo/model/format.js"
  "use strict";

  return function(data) {
    // this === control instance
  };
}, /*export*/true); // <-- Enables accessing this module via global name "demo.model.format"

Controller

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "demo/model/format" // trigger defining the format function module
], function(Controller) {
  // ...

  return Controller.extend("...", {
    // formatter: formatter <-- remove it!
  });
});


来源:https://stackoverflow.com/questions/54290194/how-to-have-control-instance-as-this-in-formatter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!