设置新模型后,Formatter 参数被传递为 null

Formatter parameter is being passed null after setting a new model

在我的 XML 列表对象中,我使用了格式化程序:

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

这按预期工作,我可以看到 Erdat 的绑定值传递给 dateFormatter,然后适当地格式化。但是,在我的应用程序上,我可以选择重新调用后端网关服务,这会将新获取的数据重新绑定到列表 (listLogs):

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

这有效,我可以看到正在从服务中获取新数据。但是,当我将这个新模型设置为 listLogs 时,格式化程序被命中,但这次它被传递 null,然后当格式化程序尝试使用这个值做任何事情时崩溃。

我已经调试并看到在第二次调用时获取的数据不为空,那么为什么将空值传递给格式化程序?

如果非要我猜的话,我会说绑定中的任何更改都会触发格式化程序。这也可能是 'unbind' 在您绑定新模型之前发生的。或者您在实际加载数据之前绑定新模型。

为什么不在格式化程序中做这样的事情:

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

最好让你的格式化程序 return 有用(至少是一个空字符串),即使它的输入很糟糕,这很容易由于不同的原因(模型尚不存在,数据尚未加载) ,...)。这将防止您的 UI 显示像 'undefined'.

这样丑陋的东西

顺便说一句:您是否考虑过使用 Types 进行日期格式化?

BR 克里斯

I (...) re-bind the newly fetched data to the List.

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

也许这根本不是必需的,假设可以将 ActivityLogsSet 直接绑定到列表。之后你可以调用 refresh from ODataListBinding.

例子

<List id="listLogs" items="{/ActivityLogsSet}"> <!-- in place of oActLogs.read() -->

(聚合绑定负责发送请求和设置数据到List。)

然后在控制器的某处:

onRefreshButtonPress: function() {
  this.byId("listLogs").getBinding("items")/*ODataListBinding*/.refresh();
},

绑定发送新请求后(refresh()),接收到数据时将触发格式化程序,但使用适当的值而不是null 从头开始​​。


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

您甚至可能不需要自定义 dateFormatter。参见