cancel
Showing results for 
Search instead for 
Did you mean: 

Formatter called multiple times from a XML view

claudino
Explorer
0 Kudos

Hello,
i created a formatter to show a text from an "i18n" model.

When the page (view) starts, the function "ageDescription" inside of the formatter is called 3 times:

  1. sBirthday is "undefined"
    sFormat is "undefined"
  2. sBirthday is valorized correctly
    sFormat is "undefined"
  3. sBirthday is valorized correctly
    sFormat is valorized correctly

Why this happen?
Is correct?
How i can handle a function not managing "undefined" values?

formatter.js

sap.ui.define([
	"tools/moment"
], function (DateFormat) {
	"use strict";
	return {
		ageDescription : function (sBirthday, sFormat) {			
			var oBundle = this.getView().getModel("i18n").getResourceBundle();
			var sAge = oBundle.getText("tooYoung");
			var sFromMyBd = moment(sBirthday, sFormat).fromNow();
			do.something();
			return sAge;
		}
	}
});

main.controller.js

sap.ui.define([
   "sap/ui/core/mvc/Controller",
   "sap/ui/model/json/JSONModel",
   "sap/ui/model/resource/ResourceModel",
   "sap/m/library",
   "model/formatter"
], function (Controller, JSONModel, ResourceModel, mobileLibrary, formatter) {
	"use strict";
	return Controller.extend("controller.main", {
		formatter: formatter,
		onInit : function () {
			do.something();
		}
 });
});

main.view.xml

<mvc:View
	controllerName="controller.main"
	xmlns="sap.m"
	xmlns:mvc="sap.ui.core.mvc"
	xmlns:l="sap.ui.layout"
	xmlns:f="sap.ui.layout.form">

	<Label text="{ 
		parts: [ 
			{path: '/user/birthday'}, 
			{path: '/user/dateFormat'} 
		], 
		formatter: '.formatter.ageDescription' }"
	class="sapUiSmallMargin"/>

</mvc:View>

Accepted Solutions (0)

Answers (2)

Answers (2)

devendervb
Contributor
0 Kudos

Hello Claudio Vicenti ,

Can you check whether the binding is run before Model is read the data.

I have seen a scenario where, binding happens on a action(eg : clicking on Button), in this case binding in view is already specified so, for first time when it tries to render, the value of binding can be undefined.

Just checking , it may help you.

Valeck
Participant
0 Kudos

hi,

my formatter is different:

.js

sap.ui.define([], function () {
	"use strict";

	return {

		formatterVisiblePrest: function (FLAG, TIPO) {
			
			var vis = false;
        	if (FLAG === "X" && TIPO !== "X"){
               vis = true;  
                      } 
			return vis;
		},
	};	
});

.xml

<Button tooltip="{i18n>AggiungiAtt}" visible="{parts: [{path: 'TabAttRV>FLAG'}, {path:'TabAttRV>TIPO'}], formatter:'.formatter.formatterVisibleAtt'}" id="idAggiungiButton" enabled="{= ${TabAttRV>FLAG} === 'X'}" press="onAggiungiAttivita"