softshipper
softshipper

Reputation: 34071

`this.oType.formatValue` is not a function

I am trying to create a custom type as following:

return {
    create: function (bSign, sUnit, iDecimal) {
        return sap.ui.model.SimpleType.extend("type.NumUnit", {

            formatValue: function (iValue, sInternalType) {
                if (bSign) {
                    const iNeg = iValue * -1;
                    return `${iNeg} ${sUnit}`;
                }
                return `${iValue} ${sUnit}`;
            },

            parseValue: function (iValue, sInternalType) {
                //Split from unit
                const [iQuantity] = iValue.split(" ");
                return Util.convertStrToFloat(iQuantity, iDecimal);
            },

            validateValue: function (iValue) {
                return iValue;
            }
        });

    }
};

and use it like:

const oNumInput = new sap.m.Input(sId, {
    maxLength: oData.NumberDigits,
    type: sap.m.InputType.Number,
    showValueHelp: oData.WithValues
});

const oType = UnitType.create(oData.Sign, oData.UnitTExt, oData.NumberDecimals);

oNumInput.bindProperty("value", {
    path: "value",
    type: oType
});
return oNumInput;

The compiler complains:

PropertyBinding-dbg.js:91 Uncaught (in promise) TypeError: this.oType.formatValue is not a function
    at constructor.P._toExternalValue (PropertyBinding-dbg.js:91)
    at constructor.P.getExternalValue (PropertyBinding-dbg.js:77)
    at f.g.updateProperty (ManagedObject-dbg.js:3367)
    at constructor.v (ManagedObject-dbg.js:3209)
    at constructor.a.fireEvent (EventProvider-dbg.js:228)
    at constructor.B._fireChange (Binding-dbg.js:271)
    at constructor.O.checkUpdate (ODataPropertyBinding-dbg.js:133)
    at constructor.O.initialize (ODataPropertyBinding-dbg.js:48)
    at f.g._bindProperty (ManagedObject-dbg.js:3310)
    at f.g.updateBindings (ManagedObject-dbg.js:4049)

What am I doing wrong?

Upvotes: 2

Views: 451

Answers (1)

Boghyon Hoffmann
Boghyon Hoffmann

Reputation: 18044

TypeError: this.oType.formatValue

This error usually occurs when the assigned type is not an instance of sap.ui.model.Type.

According to your code, SimpleType.extend returns a module that you still have to create an instance from. So you cannot just assign the module to the type: when binding property. You still need to call the constructor with new:

const Type = UnitType.create(oData.Sign, oData.UnitTExt, oData.NumberDecimals);
oNumInput.bindProperty("value", {
  path: sPath, /*E.g.: "CharInput>/ZPM_TEST_7" and NOT a static value*/
  type: new Type()
});

Then, a new instance is created which should have the formatValue method.

Upvotes: 2

Related Questions