SAPUI5 - bindElement第二次不起作用

时间:2017-05-19 16:20:38

标签: path sapui5

我调用视图的bindElement来执行webservice并获取数据。 如果tha路径的密钥不同,则会相应地执行呼叫。 事件“dataReceived”未在同一路径的第二次触发。

示例:

  • 第一次:

我用路径'ABCD'调用bindElement,它正在工作,dataReceived是trigerred。

  • 第二次:

如果我调用相同的路径'ABCD',注意到了,则事件dataReceived没有触发。

如果我将另一条路径称为“EFGH”,那么它正在运行,而dataReceived则是trigerred。

那么,即使路径相同,我还能用bindElement触发事件呢?

感谢。

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);

					}

				}
			}
		});

2 个答案:

答案 0 :(得分:2)

仅在收到数据时才会触发DataReceived。因此,不会请求第二次数据,因此不会触发dataReceived。

为此使用“更改”事件。

作为这里涉及的三个事件的例子,按照它们被解雇的顺序。

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.
    }
}

答案 1 :(得分:0)

如果您使用相同路径拨打bindElement两次,则第二次实际上不会触发新呼叫以获取新数据,因为路径没有变化。由于不会成为第二个电话,因此不会成为第二个dataReceived事件。

如果要再次触发,可以手动触发。

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

根据您的代码,看起来您在获得响应时尝试从服务器执行代码。我会使用EventProvider类中的attachEventOnce方法。

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

requestCompleted事件将在数据返回一次后触发,然后再次清除事件,这样您就不会通过相同的回调函数运行每个请求的每个响应。