简体   繁体   English

SAPUI5 在格式化程序中使用格式化程序

[英]SAPUI5 Use formatter inside formatter

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.但是,我的格式化程序 (formatter.roundNumberToTwoDecimals) 未定义。
Does anyone know a solution to this?有谁知道这个问题的解决方案?

Thanks.谢谢。

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.函数 roundToDecimal 只能由格式化程序中的函数访问。 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”的上下文无关紧要,因为它从 jsview 更改为 xml 视图。

this.formatter.roundNumberToTwoDecimals(dNumber);

工作正常(这是视图的控制器)

试试 this.roundNumberToTwoDecimals(dNumber)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM