我有兩個模型定義在我的Component.js。一個是所有聯繫人的列表,另一個只是登錄的聯繫人。現在我想檢查我的控制器,如果登錄的聯繫人已經存在於所有聯繫人列表中。我從列表中重新比較登錄聯繫人的令牌中的registrationToken。但是當我通過列表循環時,由於異步通信,長度爲0。 只見attachRequestCompleted功能,但現在我有一個問題......當我的附加功能是填補我的視圖模型的OnInit的功能已經完成..SAPUI5/OpenUI5:模型attachRequestCompleted onInIn函數
onInit : function(){
var gLocalContact = sap.ui.getCore().getModel("gLocalContact");
var gRemoteContacts = sap.ui.getCore().getModel("gRemoteContacts");
gRemoteContacts.attachRequestCompleted(function() {
... if ... setProperty to gLocalContact.getProperty("/registrationToken")...
console.log("I should be the first log to get the data in view");
});
console.log("I should be the second log!");
this.getView().setModel(gLocalContact, "localContact");
}
第一個日誌中附上功能應該是第一個因爲我在那裏定義了一些數據到gLocalContact我需要在我看來。另一個問題是,我無法訪問我的gLocalContact變量....
感謝您的回答,我嘗試過,但我還沒有解決它。我發現,當我只記錄json模型時,我可以看到數據。當我使用* console.log(jsonModel.oData); *或* console.log(jsonModel.oData.length); *它的一個空對象或未定義。當我在我的attachRequestCompleted函數中使用它時,它工作的很好,但有jsonModel2.getProperty()爲空。 – WhoAmIReally
也許第二個模型尚未完成加載時,第一個模型?你有沒有試過我的第三個解決方案有了承諾,您可以輕鬆等待多個異步請求。 – schnoedel
我試過你的JSBin,但是我還沒有在我的項目中實現它...我會試着理解它是如何工作的 – WhoAmIReally