如何在没有 oEvent 的情况下处理调用函数 [英] How to handle calling a function without oEvent
问题描述
我有一个 CheckBox,带有附加到 select
事件的处理程序.在这个函数中是动态填充/显示几个字段的代码.如果我出现在屏幕上并且数据引入了一个值,该值使复选框已被选中,则不会显示这些字段(因为它们仅在我选中复选框时才可见).
I have a CheckBox with a handler attached to the select
event. In this function is the code to dynamically populate/ display few fields. If I come on the screen and the data brings in a value which makes the checkbox selected already, then those fields are not displayed (because they become visible only when I select the checkbox).
我想确保如果 CheckBox 是自动选择的,我仍然应该能够处理函数中的逻辑,该函数将 oEvent
作为输入参数.但问题是,如果我从另一个方法调用此函数,该函数将不起作用,因为它有许多诸如 oEvent().getSource()
之类的语句,我没有通过.
I want to ensure that if the CheckBox is auto selected, still I should be able to process the logic in the function, which has oEvent
as an input parameter. But the issue is that if I call this function from another method, that function does not work as it has many statements like oEvent().getSource()
which I do not pass.
onCheckBoxSelect: function(oEvent) {
var cells = sap.ui.getCore().byId("cell");
controlCell.destroyContent();
vc.abc();
var material= sap.ui.getCore().byId("abc");
var isSelected = oEvent.getParameters("selected").selected;
if (isSelected) {
// ...
}
},
someFunction : function(){
if(true){
// want to call onCheckBoxSelect here
}
// ...
},
推荐答案
将处理程序主体解耦为一个单独的函数,以便其他函数可以使用正确的参数调用解耦的函数.例如:
Decouple the handler body into a separate function so that other functions can call the decoupled function with the right arguments. For example:
onCheckBoxSelect: function(oEvent) {
const bSelected = oEvent.getParameter("selected");
this.doIt(bSelected); // Instead of "doing it" all here
},
someFunction: function(){
if (/*Something truthy*/) {
const checkBox = this.byId("myCheckBox");
const bSelected = checkBox.getSelected();
doIt(bSelected); // passing the same argument as in onCheckBoxSelect
}
// ...
},
doIt: function(bSelected) { // decoupled from onCheckBoxSelect
// ...
if (bSelected) {
// ...
}
},
查看
<CheckBox id="myCheckBox"
select=".onCheckBoxSelect"
/>
或从 1.56 开始:
<CheckBox id="myCheckBox"
select=".doIt(${$parameters>/selected})"
/>
Docu:处理 XML 视图中的事件
这样,您就可以拥有一个可以从任何地方调用的纯解耦函数.
By that, you can have a pure, decoupled function that can be called from anywhere.
这篇关于如何在没有 oEvent 的情况下处理调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!