hy13
hy13

Reputation: 115

SAPUI5 - Model.Remove generates two requests

I am currently facing a strange behavior with my SAPUI5 coding when I do a DELETE with the model (sap.ui.model.odata.v2.ODataModel). I wanted to implement a list, which displays some "Favorites" in a SelectDialog. By pressing the icon, the users can delete a favorite. For the item itself I used a FeedListItem, which is triggering the iconPress-Event _handleIconPressDelete.

<FeedListItem icon="sap-icon://delete" iconActive="true" iconPress="_handleIconPressDelete" text="{Name}" sender="{ID}"/>

The event looks like this:

    _handleIconPressDelete: function(oEvent) {
        var oModel = oEvent.getSource().getModel();

        oModel.remove(oEvent.getSource().getBindingContext().getPath(), {
            success: function(data) {
                // success handling
            },
            error: function(e) {
                // error handling
            }
        });
    }

But when this event is triggered, two identical delete requests are generated and causing an error, because with the current changeset coding in the backend, I am only allowed to do one request at the same time.

The strange thing is, this behavior only appears when I open the dialog the first. When I close and reopen it, everything works fine.

Do you have any ideas, what I might do wrong here so that two requests are generated? I also checked, if the event is triggered multiple times, but that wasn't the case.

As current workaround I am using deferredGroups as shown in the snipped below so that the two request are separated, but I think there must be better ways to solve this.

_handleIconPressDelete: function(oEvent) {
        var oModel = oEvent.getSource().getModel();

        oModel.setDeferredGroups(["group1"]);

        oModel.remove(oEvent.getSource().getBindingContext().getPath(), {
            groupId: "group1",
            success: function(data) {
                // success handling
            },
            error: function(e) {
                // error handling
            }
        });

        oModel.submitChanges({
            groupId: "group1"
        });
    }

Upvotes: 1

Views: 3585

Answers (1)

F  H
F H

Reputation: 125

I too experienced the same issue where the event associated with iconPress of FeedListItem triggers twice though user click only once..

Following is a workaround which you can implement using custom coding.

  1. Declare the following variable in view controller's onInit()

    this._bFirstTrigger = true;//SETTING FOR THE FIRIST TIME
    
  2. Use this in FeedListItem's iconPress event to ensure that the relevant code executes only once as follows:

    _handleIconPressDelete: function(oEvent) {
    if (this._bFirstTrigger) {
    var oModel = oEvent.getSource().getModel();oModel.setDeferredGroups(["group1"]);
    
    oModel.remove(oEvent.getSource().getBindingContext().getPath(), {
        groupId: "group1",
        success: function(data) {
            // success handling
        },
        error: function(e) {
            // error handling
        }
    });
    
    oModel.submitChanges({
        groupId: "group1"
    });
    }
    this._bFirstTrigger = false;
    }
    else{
     this._bFirstTrigger = true;
    }
    

Regards,

Fahad Hamsa

Upvotes: 1

Related Questions