问题
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