Search code examples
sapui5

How to access other function in UI5 custom control/type?


I defined many custom type in Utils.js: http://plnkr.co/edit/BGqDoWEC7DTmomEFHygd?p=catalogue

I want to add trim() in parseValue

parseValue: function (oValue) {
    if(oValue !== null) {
        return oValue.trim();
    }
    return oValue;
},

Copy and paste this function is kind of dumb, so I want to do sth. like :

trimString : function(oValue) {
    if(oValue !== null) {
        return oValue.trim();
    }
    return oValue;
},

mandatoryValueType : SimpleType.extend("text", {
    formatValue: function (oValue) {
        return oValue;
    },
    parseValue: this.trimString,
    validateValue: function (oValue) {
        if (!oValue) {
            throw new ValidateException(Utils.i18n("MANDATORY_VALIDATE_ERROR"));
        }  
    }
}),

But scope in mandatoryValueType seems can not access to this.trimString, what can I do?

this scope in parseValue function, no trimString function: enter image description here

Another working sample : http://plnkr.co/edit/ahS6NlUHHL0kdvdddvgd?p=preview Reference: https://sapui5.hana.ondemand.com/#/sample/sap.m.sample.InputChecked/preview


Solution

  • Maybe you can put the trimString function outside the var Utils object, and set it as a global function.

    here is the example: http://plnkr.co/edit/u64DEP0RnT5CJADZyXJg?p=catalogue

    edit:

    corrected by @boghyon, the trimString function should be a function in a closure, instead of a global function :)