i am trying to access a formatter-method from another formatter-method, like this:
sap.ui.define(["<package>/model/formatter"], function(formatter) {
"use strict";
return {
formatter: formatter,
roundNumberToTwoDecimals: function(number) {
if (number) {
return number.toFixed(2);
}
},
stringFormatter: function(sI18n, dNumber, sProduct) {
var i18n = this.getModel("i18n");
if (i18n) {
return i18n.getText(sI18n, [formatter.roundNumberToTwoDecimals(dNumber), sProduct]);
}
}
};
However, my formatter (formatter.roundNumberToTwoDecimals) is undefined.
Does anyone know a solution to this?
Thanks.
i am trying to access a formatter-method from another formatter-method, like this:
sap.ui.define(["<package>/model/formatter"], function(formatter) {
"use strict";
return {
formatter: formatter,
roundNumberToTwoDecimals: function(number) {
if (number) {
return number.toFixed(2);
}
},
stringFormatter: function(sI18n, dNumber, sProduct) {
var i18n = this.getModel("i18n");
if (i18n) {
return i18n.getText(sI18n, [formatter.roundNumberToTwoDecimals(dNumber), sProduct]);
}
}
};
However, my formatter (formatter.roundNumberToTwoDecimals) is undefined.
Does anyone know a solution to this?
Thanks.
Share edited Jan 8, 2021 at 21:06 Sandra Rossi 13.8k6 gold badges25 silver badges56 bronze badges asked Oct 22, 2015 at 7:30 www40www40 2852 gold badges7 silver badges19 bronze badges3 Answers
Reset to default 3You can define helper functions in private scope.
sap.ui.define(["<package>/model/formatter"], function() {
"use strict";
//private scope
var roundToDecimal = function(iNumber,iFixed){
return iNumber.toFixed(iFixed);
}
return {
roundNumberToTwoDecimals: function(number) {
if (number) {
return roundToDecimal(number,2);
}
},
stringFormatter: function(sI18n, dNumber, sProduct) {
var i18n = this.getModel("i18n");
if (i18n) {
return i18n.getText(sI18n, [roundToDecimal(dNumber,2), sProduct]);
}
}
};
The function roundToDecimal can be accessed only by the functions within the formatter. It can't be accessed directly as a formatter function from a view as it's not supposed to be exposed as a formatter function but just a helper function. With this way, the context of 'this' passed to the formatter doesn't matter as it changes from jsview to xml view.
this.formatter.roundNumberToTwoDecimals(dNumber);
works fine (this is the controller of the view)
try this.roundNumberToTwoDecimals(dNumber)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745067953a4609369.html
评论列表(0条)