为什么格式化程序在片段 xml 中不起作用?
why formatter doesn't work in fragment xml?
我的其他库中有一个格式化函数,我的目标是使用这个函数。这是我的 Formatter.js :
sap.ui.define([], function () {
"use strict";
var Formatter = {
showAsDate : function(str){...}
};
return Formatter;
});
在我的片段中,我正在使用它,如下所示:
<Text text="{
path: 'jsonModel>/date',
formatter : '.Formatter.showAsDate'
}" />
然后我在控制器中调用 Formatter.js,如下所示:
sap.ui.define(["libpath/Formatter"], function (Formatter){
"use strict";
return Controller.extend("controller.myController", {
Formatter : Formatter,
//I add my fragment to myView with this method
//and I am calling this method in onInit.
_showFormFragment : function (sFragmentName) {
var oPage = this.getView().byId("page");
oPage.removeAllContent();
oPage.insertContent(this._getFormFragment(sFragmentName));
}
});
});
我正在使用 openui5beta library.And I coded my view and controllers according to the walkthrough sapmle. And you can see similar usage in InvoiceList.controller.js, InvoiceList.view.xml、HelloDialog.fragment.xml
。但是这些示例显示了如何从视图而不是片段到达格式化程序。我可以从 myView.view.xml 到达 Formatter.js 但我无法从我的 fragment.Any 到达它,我们将不胜感激。
你能分享一下你的片段是如何被调用的吗?
通常在调用片段时,您有一个额外的参数来指定 JS 对象上下文,如下所示
var oFragment = sap.ui.xmlfragment("YourFragment.xml",this);
以上代码是在控制器中调用的,因此 "this" 将引用控制器实例并且格式函数应该可以工作。
我的其他库中有一个格式化函数,我的目标是使用这个函数。这是我的 Formatter.js :
sap.ui.define([], function () {
"use strict";
var Formatter = {
showAsDate : function(str){...}
};
return Formatter;
});
在我的片段中,我正在使用它,如下所示:
<Text text="{
path: 'jsonModel>/date',
formatter : '.Formatter.showAsDate'
}" />
然后我在控制器中调用 Formatter.js,如下所示:
sap.ui.define(["libpath/Formatter"], function (Formatter){
"use strict";
return Controller.extend("controller.myController", {
Formatter : Formatter,
//I add my fragment to myView with this method
//and I am calling this method in onInit.
_showFormFragment : function (sFragmentName) {
var oPage = this.getView().byId("page");
oPage.removeAllContent();
oPage.insertContent(this._getFormFragment(sFragmentName));
}
});
});
我正在使用 openui5beta library.And I coded my view and controllers according to the walkthrough sapmle. And you can see similar usage in InvoiceList.controller.js, InvoiceList.view.xml、HelloDialog.fragment.xml 。但是这些示例显示了如何从视图而不是片段到达格式化程序。我可以从 myView.view.xml 到达 Formatter.js 但我无法从我的 fragment.Any 到达它,我们将不胜感激。
你能分享一下你的片段是如何被调用的吗? 通常在调用片段时,您有一个额外的参数来指定 JS 对象上下文,如下所示
var oFragment = sap.ui.xmlfragment("YourFragment.xml",this);
以上代码是在控制器中调用的,因此 "this" 将引用控制器实例并且格式函数应该可以工作。