2011-07-21 21 views
2

之前,我有以下的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數組中沒有數據?

回答

2

這樣的事情會是更好的方式(IM不能保證這是100%的工作,但概念是聲音):

function eventTypeObj() { 
    this.allEventTypes = []; 
    this.hasLoadedEventTypes = false; 

    var loadEventTypes = function(cb) { 
     $.ajax({ 
      url: "/ATOMWebService.svc/GetDisplayEventTypes", 
      dataType: "json", 
      success: function(result) { 
       this.allEventTypes = eval("(" + result.d + ")"); 
       this.hasLoadedEventTypes = true; 
       cb(); 
      } 
     }); 
    }; 


    this.getEventTypeIds = function(updateEventTypes, callback) { 
     var _getEventTypeIds = function() { 
      var eventTypeIDs = []; 
      for (var i = 0; i < this.allEventTypes.length; i++) { 
       eventTypeIDs.push(this.allEventTypes[i].Id); 
      }  
      return eventTypeIDs; 
     }; 


     if (!this.hasLoadedEventTypes || updateEventTypes) { 
      loadEventTypes(function(){ callback(_getEventTypeIds()); }); 
     } 
     else callback(_getEventTypeIds()); 
    }; 
} 

用法示例:

var eto = new eventTypeObj(); 

eto.getEventTypeIds(false, function(eventTypeIdArray) { 
    // do stuff with the id array 
}); 


/* 
    somewhere later on you want to get an updated eventTypeId array 
    in case the event types have changed. 
*/ 
eto.getEventTypeIds(true, function(eventTypeIdArray) { 
    // do stuff with the updated ids 
}); 
1

沒有辦法防止有人太快調用它。如果他們過早打電話,你會希望發生什麼?

它看起來像你的代碼現在當前返回一個空數組,如果allEventTypes還沒有被填充。你可以決定這個空數組是否是正確的結果,或者當它被稱爲過早時應該拋出一個異常這對調用者來說絕對清楚,數據尚未提供。

您可以提供誰需要這些信息的人一些輔助代碼,但它可能沒有用。例如,你可以讓他們註冊一個回調會得到來自後的數據已經填補在成功處理程序調用。你可以讓他們查詢是否是可用的數據呢。

如果你不想要的時機是在調用者的責任,那麼你不能提供一個同步的方式來獲得這些信息。相反,你只會提供一個回調機制來獲取數據。如果數據準備就緒,該回調將立即被調用。如果數據沒有準備好,當ajax函數完成時,回調會被調用。在任何一種情況下,調用者都必須僅在回調中處理數據,並且getEventTypeIds不會像現在那樣是一種正常的調用來獲取數據,而是調用一個調用來註冊將在數據被調用時調用的回調準備。這將不必知道什麼時候該數據準備的實施細則緩解來電,但將迫使他們使用回調機制的異步特性。

this.getEventTypeIds = function(callback) { 
    if (allEventTypes.length > 0) { 
     // data is ready call the callback with the data now 
    } else { 
     // store the callback to be called later from the success handler 
    } 
} 
+0

是啊,我想通儘可能多。我確實想過要返回某種類型的錯誤代碼,但它似乎將對象的實現細節公開給調用者,並將處理問題的責任移交給調用者。有沒有更好的方法將這種東西封裝在ajax/data對象的Java腳本中? –

+1

我增加了更多的信息,以我的回答對我所知道的唯一的方式,以減輕數據到達的定時任何責任的調用者。 – jfriend00

+0

+1給jfriend解釋它。就在他更新他的答案之前,我發佈了回調風格邏輯的完整實現。看一看。 – jdc0589

0

你可以檢查eventType數組是否爲空,對不對?

if(allEventTypes.length == 0) 
{ 
    return; 
} 
1
var allowCall = false; 
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 + ")"); 
      allowCall = true; 
     } 
    }); 


// Returns a list of all the event type IDS. 
this.getEventTypeIds = function() { 
    if(!allowCall) return; // or pop up a message 
    var eventTypeIDs = []; 
    for (var i = 0; i < allEventTypes.length; i++) { 
     eventTypeIDs.push(allEventTypes[i].Id); 
    } 
    return eventTypeIDs; 
}; 
} 

或者只是檢查是否allEventTypes爲空。

相關問題