Javascript SAPUI5本地作用域对象作为格式化程序函数参数
本地作用域对象如何作为参数传递到格式化程序函数中。我只知道如何将值从绑定传递到formatter函数,但我需要将其他对象从本地范围传递到formatter函数Javascript SAPUI5本地作用域对象作为格式化程序函数参数,javascript,formatting,sapui5,Javascript,Formatting,Sapui5,本地作用域对象如何作为参数传递到格式化程序函数中。我只知道如何将值从绑定传递到formatter函数,但我需要将其他对象从本地范围传递到formatter函数 // request the order operations and bind them to the operation list oView.getModel().read(sPath + "/OperationSet", { success: func
// 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");
}
});
你可以:
还绑定到与对象中包含相同信息的其他值
创建一个闭包来捕获对象并使其在格式化程序中可用。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))
将信息存储在模型中的某个位置,并在格式化程序函数中访问该模型(尽管有点反模式)您可以:
还绑定到与对象中包含相同信息的其他值
创建一个闭包来捕获对象并使其在格式化程序中可用。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))
将信息存储在模型中的某个位置,并在格式化程序函数中访问该模型(尽管有点反模式)您可以:
还绑定到与对象中包含相同信息的其他值
创建一个闭包来捕获对象并使其在格式化程序中可用。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))
将信息存储在模型中的某个位置,并在格式化程序函数中访问该模型(尽管有点反模式)您可以:
还绑定到与对象中包含相同信息的其他值
创建一个闭包来捕获对象并使其在格式化程序中可用。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))
将信息存储在模型中的某个位置,并在格式化程序函数中访问该模型(尽管有点反模式)您只需从格式化程序上下文中调用本地对象即可 假设你有:
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”
如果您谈论的是位于完全不同位置(例如,不同的视图或控制器)的局部变量,则必须更具体地说明从何处到何处需要变量,以获得最佳答案。您可以从格式化程序上下文中调用局部对象 假设你有:
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”
如果您谈论的是位于完全不同位置(例如,不同的视图或控制器)的局部变量,则必须更具体地说明从何处到何处需要变量,以获得最佳答案。您可以从格式化程序上下文中调用局部对象 假设你有:
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”
如果您谈论的是位于完全不同位置(例如,不同的视图或控制器)的局部变量,则必须更具体地说明从何处到何处需要变量,以获得最佳答案。您可以从格式化程序上下文中调用局部对象 假设你有:
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”
如果您谈论的是位于完全不同位置(例如,不同的视图或控制器)的局部变量,然后,为了获得最佳答案,您必须更明确地说明变量从何处到何处。myobject是常量,与绑定值无关?不使用其他视图信息创建myobject。myobject是常量,与绑定值无关?不使用其他视图信息创建myobject。myobject是否与绑定值无关?是否使用其他视图信息创建MyObject?是否与绑定值无关?是否使用其他视图信息创建MyObject?是否使用其他视图信息创建MyObject。只有在同一功能块内联实现formatter函数时,此结论才有效。我修改了源代码来表示问题。我给你+1是因为解决方案适用于我之前的示例代码。在本例中,我将执行以下操作:更改“sOrderType”后,将其作为属性添加到模型中(oModel.setProperty(sNewPath,sOrderType));在格式化程序函数中,绑定部分(例如此答案),其中一个路径设置为新设置的属性。每次属性更改时,formatter函数都会自动更新。当未设置任何内容时,字符串将返回undefined,否则字符串将返回字符串值。只有在同一个函数块中内联实现formatter函数时,此结论才有效。我修改了源代码来表示问题。我给你+1是因为解决方案适用于我之前的示例代码。在本例中,我将执行以下操作:更改“sOrderType”后,将其作为属性添加到模型中(oModel.setProperty(sNewPath,sOrderType));在格式化程序函数中,绑定部分(例如此答案),其中一个路径设置为新设置的属性。每次属性更改时,formatter函数都会自动更新。当未设置任何内容时,字符串将返回undefined,否则字符串将返回字符串值。只有在同一个函数块中内联实现formatter函数时,此结论才有效。我修改了源代码来表示问题。我给你+1是因为解决方案适用于我之前的示例代码。在本例中,我将执行以下操作:更改“sOrderType”后,将其作为属性添加到模型中(oModel.setProperty(sNewPath,sOrderType));在格式化程序函数中,绑定部分(例如此答案),其中一个路径设置为新设置的属性。每次属性更改时,formatter函数都会自动更新。当未设置任何内容时,字符串将返回undefined,否则字符串将返回字符串值。只有在同一个函数块中内联实现formatter函数时,此结论才有效。我修改了源代码来表示问题。我给你+1,因为这个解决方案对