本地范围对象如何作为参数传递给格式化程序函数。我只知道如何将绑定中的值传递给formatter函数,但我需要将其他对象从local scope传递到formatter函数。
// request the order operations and bind them to the operation list
oView.getModel().read(sPath + "/OperationSet", {
success: function (oData, oResponse) {
if (oData && oData.results) {
var oOrder = oView.getBindingContext().getObject();
// I need sOrderType inside the external formatter function
var sOrderType = oOrder.OrderType;
operationList.bindAggregation("items", {
path: sPath + "/OperationSet",
template: new sap.m.StandardListItem({
title: "{Description}",
info: "{DurationNormal} {DurationNormalUnit}",
visible: {
parts: [{path: 'Activity'}],
formatter: de.example.util.Formatter.myFormatterFunction
}
}),
filters: []
});
}
},
error: function (oData) {
jQuery.sap.log.error("Could not read order operations");
}
});
答案 0 :(得分:2)
你可以:
还绑定到包含与oMyObject
中的信息相同的信息的其他值创建一个闭包来捕获oMyObject并使其在formatter中可用。 e.g:
formatter: ((function(oObject){
//oObject is available inside this function
return function (firstName, lastName, amount, currency) { // all parameters are strings
if (firstName && lastName && oObject.someProperty == null) {
return "Dear " + firstName + " " + lastName + ". Your current balance is: " + amount + " " + currency;
} else {
return null;
}
};
})(oMyObject))
将信息存储在模型中某处的oMyObject中,并在formatter函数中访问该模型(尽管有点反模式)
答案 1 :(得分:1)
您只需从格式化程序上下文中调用本地对象即可。
说你有:
var sString = "Test";
oTxt.bindValue({ parts: [{
path: "/firstName",
type: new sap.ui.model.type.String()
}, {
path: "/lastName",
type: new sap.ui.model.type.String()
}, {
path: "/amount",
type: new sap.ui.model.type.Float()
}, {
path: "/currency",
type: new sap.ui.model.type.String()
}], formatter: function (firstName, lastName, amount, currency) {
console.log(sString);
// all parameters are strings
if (firstName && lastName) {
return "Dear " + firstName + " " + lastName + ". Your current balance is: " + amount + " " + currency;
} else {
return null;
} } });
这将正确地将sString记录为“test”。
如果你在谈论一个完全不同的地方的变量(例如不同的视图或控制器),那么你必须更具体地说明你想要变量的位置以获得最佳答案