之前,我有以下的Java腳本對象我怎麼能阻止一個對象的方法調用Ajax的完成
function eventTypeObj() {
allEventTypes = [];
// When the object is created go and get all the event types that can be included in journey or clusters.
$.ajax({
url: "/ATOMWebService.svc/GetDisplayEventTypes",
dataType: "json",
success: function(result) {
allEventTypes = eval("(" + result.d + ")");
}
});
// Returns a list of all the event type IDS.
this.getEventTypeIds = function() {
var eventTypeIDs = [];
for (var i = 0; i < allEventTypes.length; i++) {
eventTypeIDs.push(allEventTypes[i].Id);
}
return eventTypeIDs;
};
}
我想知道是否有一種方法阻止一些一個調用eventTypeObj.getEventTypeIds();在構造函數中的ajax調用成功之前,allEventTypes數組中沒有數據?
是啊,我想通儘可能多。我確實想過要返回某種類型的錯誤代碼,但它似乎將對象的實現細節公開給調用者,並將處理問題的責任移交給調用者。有沒有更好的方法將這種東西封裝在ajax/data對象的Java腳本中? –
我增加了更多的信息,以我的回答對我所知道的唯一的方式,以減輕數據到達的定時任何責任的調用者。 – jfriend00
+1給jfriend解釋它。就在他更新他的答案之前,我發佈了回調風格邏輯的完整實現。看一看。 – jdc0589