Reputation: 285
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.
Upvotes: 1
Views: 4706
Reputation: 929
You 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.
Upvotes: 3
Reputation: 285
this.formatter.roundNumberToTwoDecimals(dNumber);
works fine (this is the controller of the view)
Upvotes: 3