0

I have the following function where I call an OData service to validate input and want to clear the source component if the value is not valid.

After calling the service, it does not recognize the oEvent.getSource().setValue in the success callback and I'm unable to clear the component.

validateEquipment: function (oEvent) {
    var _this = this;
    var circ7 = _this._oComponent.getModel("LoggedInUser").getData().circ7;
    var equipmentNumber = oEvent.getSource().getValue();
    var equipmentType = "1";
    if (oEvent.getParameter("id").indexOf("containerID") > -1) {
        equipmentType = "2";
    }
    var module = "";
    if (circ7 && equipmentNumber.length === 10) {
        _this._oComponent.getModel("serviceMetaData").read("Equipment_Look(EquipmentType='" + equipmentType + "',EquipmentNumber='" + equipmentNumber + "',Module='" + module + "',Circ7='" + circ7 + "')", {
            success: function (data) {
                if (data && data.ErrorMessage === "") {
                    // ...
                } else if (data) {
                    oEvent.getSource().setValue('');
                    MessageBox.error(data.ErrorMessage);
                }
            },
            // ...
        })
    } else {
        oEvent.getSource().setValue("");
        MessageBox.error("Equipment " + equipmentNumber + " is invalid.");
    }
}
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Mike
  • 41
  • 1
  • 4
  • 1
    Does this answer your question? [Event parameter overwritten after another event?](https://stackoverflow.com/questions/47452986/event-parameter-overwritten-after-another-event) – Boghyon Hoffmann Aug 19 '21 at 07:41
  • As explained in the above linked answer, *event* is a pooled object. You should store the control reference (`oEvent.getSource()`) **outside** of the `success` callback in a separate variable and use that var later in your handler. Your `oEvent` is being reused by UI5 which is the intended behavior / by design to avoid creating an event object over and over again. – Boghyon Hoffmann Aug 19 '21 at 09:20

1 Answers1

0

That's a bug in ui5. Events are reused/destroyed somewhere,if another event is happening.


Update: apparently it's not a bug. It's a feature. Ui5 is pooling event objects for no obvious reasons.


validateEquipment: function(oEvent) {
        var _this = this;
        var oSource = oEvent.getSource():
        //_this._oComponent.commonDialog.openBusyDialog();
        var circ7 = _this._oComponent.getModel('LoggedInUser').getData().circ7;
        var equipmentNumber = oSource.getValue();
        var equipmentType = "1";
        if(oEvent.getParameter('id').indexOf("containerID") > -1)
        {
            equipmentType = "2";
        }
        var module = "";
        if (circ7 && equipmentNumber.length === 10) {
            _this._oComponent.getModel("serviceMetaData").read("Equipment_Look(EquipmentType='"+equipmentType+"',EquipmentNumber='"+equipmentNumber+"',Module='"+module+"',Circ7='"+circ7+"')", {
                success: function(data) {
                    if (data && data.ErrorMessage === "") 
                    {
                    } 
                    else if(data) {
                        oSource.setValue('');
                        MessageBox.error(data.ErrorMessage);
                    }
                },
                error: function(error) {
                    MessageBox.error("Failed to load Job Code Data");
                }
            })
        }
        else
        {
            oSource.setValue('');
            MessageBox.error("Equipment " + equipmentNumber + " is invalid.");
        }
    },
Benedikt Kromer
  • 711
  • 6
  • 22