SAPUI5 - bindElement doesn't work the second t

2019-08-07 18:54发布

问题:

I call bindElement of the view to execute the webservice and get data. The call is executed correctelly if the key of tha path is different. The event "dataReceived" didn't trigger in the second time of the same path.

Example:

  • First Time:

I call bindElement with the path 'ABCD', it's working, dataReceived is trigerred.

  • The second time:

If I call the same path 'ABCD', noting is happend, the event dataReceived didn't trigger.

If I call another path 'EFGH', it's working and dataReceived is trigerred.

So what can I do to trigger the event with bindElement even if the path is the same ?

Thanks.

cb = this.getView().byId("cb").getValue();
vpath = "/ZDECL_INSet('"+ cb +"')";

this.getView().bindElement({
			path: vpath,
			mode: sap.ui.model.BindingMode.TwoWay,
			events: {
				dataReceived: function(rData) {

					var data = vthis.getView().getModel().getProperty(rData.oSource.sPath);
					msg = "";

					if(data.TMSG1 == 'E'){
						msg = data.Msg1;

						sap.m.MessageBox.show(msg, {
							icon: sap.m.MessageBox.Icon.ERROR,
							title: vtitle,
							actions: [sap.m.MessageBox.Action.YES],
							onClose: function(oAction) {

								oCB.focus();
								oCB.setValue(null);
							}
						}
						);

					}
					else{
						sap.m.MessageToast.show("Good", {
							duration: 2000,
							width: "200px"
						});

						oCB.focus();
						oCB.setValue(null);

					}

				}
			}
		});

回答1:

DataReceived will be fired only if data is received. So second time data will not be requested, so dataReceived won't be fired.

Use "change" event for this.

As example of the three events involved here, in the order they are fired.

events: {
    dataRequested: function(){
        //Here the code to be executed when a request to server was fired. For example, set a "waitingForData" flag to true
    },
    change: function(){
        //Here your magic to be executed everytime you do ElementBinding. For example, check if your "waitingForData" flag is false, if so, do whatever you want with the data you already have.
    },
    dataReceived: function(rData){
        //Here your logic to be executed when data from server is received. For example, set your "waitingForData" flag to false, and do whatever you want with the data have reveived.
    }
}


回答2:

If you call bindElement with the same path twice, the second time won't actually trigger a new call to get new data, since the path didn't change. Since there won't be a second call, there won't be a second dataReceived event.

You can fire it manually if you want to trigger it again.

this.getView().getElementBinding().fireDataReceived()

Based on your code, it looks like you're trying to execute code from your server when you get the response. I would use the attachEventOnce method from the EventProvider class.

oModel.attachEventOnce("requestCompleted", function(oEvent) {
    //execute your code here
}, this);
this.getView().bindElement(sPath);

The requestCompleted event will fire after data comes back once, and then clear the event from happening again, that way you don't always run every response from every request through the same callback function.



标签: path sapui5