Joe
Joe

Reputation: 33

Formatter parameter is being passed null after setting a new model

In my XML for a List object I have made use of a formatter:

{path: 'Erdat', formatter: '.formatter.dateFormatter'}

This works as expected and I can see the bound value for Erdat passed into dateFormatter and then formatted appropriately. However, on my app I have an option to re-call the back-end gateway service, which will re-bind the newly fetched data to the List (listLogs):

oActLogs.read("/ActivityLogsSet", {
  success: function (oData, oResponse){
    oActivityLogsModel.setData(oData);
    listLogs.setModel(oActivityLogsModel);
    oGlobalBusyDialog.close();
  },
  // ...
});

This works and I can see the new data being fetched from the service. However, when I set this new model to listLogs the formatter is hit but this time it is being passed null, and then crashes when the formatter tries to do anything with this value.

I have debugged and seen that on this second call the data fetched is not null, so why is null being passed to the formatter?

Upvotes: 2

Views: 822

Answers (1)

cschuff
cschuff

Reputation: 5542

If I had to guess I would say that any change in the binding will trigger the formatter. That might also be an 'unbind' happening before you bind the new model. Or you bind the new model before the data is actually loaded.

Why not just do something like this in your formatter:

dateFormatter: function(oValue) {
  if (!oValue) {
    return "";
  }
  // do the real stuff
}

It is good practice to make your formatter return something useful (at least an empty string) even if it's inputs are crappy which can easily happen for different reasons (model not yet exists, data not yet loaded, ...). This will prevent your UI from showing ugly stuff like 'undefined'.

Btw: did you consider using Types for date formatting?

BR Chris

Upvotes: 2

Related Questions