Search code examples
odatasapui5

Formatter not called despite passing the same binding path


I can't wrap my head around this. What am I overlooking?

Here is a minimal sample: https://plnkr.co/edit/VjqGeG9JpHblyLBb?preview

<tnt:InfoLabel
 text="{
   path: 'LastName',
   formatter: '.formatter.typeText'
 }" 
 colorScheme="{
   path: 'LastName',
   formatter: '.formatter.typeColor'
 }" />
// formatter.js
sap.ui.define([], function () {
 "use strict";
 return {
   typeText: function(sLastName) {
     // Called with 'sLastName' value
   },
   typeColor: function(sLastName) {
     // Not called
   }
 };
});

I'm using UI5 1.79 with sap.ui.model.odata.v4.ODataModel.


Solution

  • Add targetType: 'any' to the property binding info that has the issue. E.g.:

    <tnt:InfoLabel
      text="{
        path: 'LastName',
        formatter: '.getMyText'
      }"
      colorScheme="{
        path: 'LastName',
        formatter: '.getMyColorScheme',
        targetType: 'any'
      }"
    />

    With sap.ui.model.odata.v4.ODataModel, data types in property bindings are automatically determined according to the EDM type of the entity property. I.e. in the case above: even without having a certain type explicitly assigned to the text property, the v4.ODataPropertyBinding automatically picks the String type (because LastName has Type="Edm.String" in $metadata) and assigns it to the type:

    <tnt:InfoLabel
      text="{
        path: 'LastName',
        formatter: '.getMyText',
        type: 'sap.ui.model.odata.type.String' <-- automatically added by v4.ODataPropertyBinding
      }"

    This was fine for the text property since it actually awaits a string value, but doing the same for other properties, such as colorScheme which awaits an int value, results in a FormatException.*

    In order to prevent the automatic Type Determination, the targetType: 'any' has to be added.


    * With commit:4611772, which is available as of 1.80, we can see the corresponding error message in the console:

    FormatException in property 'colorScheme' of 'Element sap.tnt.InfoLabel#...': <LastName value> is not a valid int value. Hint: single properties referenced in composite bindings and within binding expressions are automatically converted into the type of the bound control property, unless a different 'targetType' is specified. targetType:'any' may avoid the conversion and lead to the expected behavior.