2013-12-11 33 views
0

我打電話給一個rest服務器的觸發站點刷新的API。然後我需要調用另一個API(另一個集合獲取)來檢查網站刷新是否完成。我需要調用第二個API,直到我得到siteRefresh =「completed」。我如何以骨幹的方式做到這一點。骨幹無限集合獲取所需響應的調用?

我的看法代碼是這樣的

define(['collections/firstCollection', 
'collections/secondCollection', 
], function(firstCollection, secondCollection) { 
var memAccId; 
var refreshView = Backbone.View.extend({ 
    tagName : 'div', 
    className : 'refresh-wrap', 
    initialize : function() {   
     var self = this;  
      self.collection = new firstCollection(); 
      self.collection.fetch({ 
       timeout : 50000, 
       type : "POST", 
       data : data,      
       success : function(response) { 
        var result = response.toJSON(); 
        if (typeof result[0].Id !== "undefined") { 
       memAccId=result[0].Id; 
        $(self.$el).trigger('onAccountId'); 
         } 

        }, 
        error : function(request, status, error) { 
         console.log("ERROR - first Collection on fetch error."); 

        } 

       });   
     }, 
     events : { 
      'onAccountId' : 'getRefreshInfo' 
     }, 
     getRefreshInfo : function() { 
     var self = this; 
      self.collection = new secondCollection(); 
      self.collection.fetch({ 
       timeout : 30000, 
       type : 'POST', 
       data : {"Id":memAccId}, 
       success : function(response) { 
      if(response.refreshStatus !=="REFRESH_COMPLETED"){ 
        // self.collection.fetch(); 
        //how to call this collection fetch until i get refreshStatus = 
"REFRESH_COMPLETED" 
      } 
      }, 
        error:function(request, status, error){ 

      } 
      }); 
      } 
     } 
    }); 
    return refreshView; 
}); 

回答

0
define(['collections/firstCollection', 
    'collections/secondCollection', 
    ], function(firstCollection, secondCollection) { 
    var memAccId; 
    var refreshView = Backbone.View.extend({ 
     tagName : 'div', 
     className : 'refresh-wrap', 
     initialize : function() {   
      var self = this;  
       self.collection = new firstCollection(); 
       self.collection.fetch({ 
        timeout : 5000, 
        type : "POST", 
        data : data,      
        success : function(response) { 
         var result = response.toJSON(); 
         if (typeof result[0].Id !== "undefined") { 
        memAccId=result[0].Id; 
//call getRefreshInfo in every 5 seconds      
    refresh = setInterval(function() { 
       self.getRefreshInfo(); 
      }, 5000); 

         }, 
         error : function(request, status, error) { 
          console.log("ERROR - first Collection on fetch error."); 

         } 

        });   
      }    
      getRefreshInfo : function() { 
      var self = this; 
       self.collection = new secondCollection(); 
       self.collection.fetch({ 
        timeout : 30000, 
        type : 'POST', 
        data : {"Id":memAccId}, 
        success : function(response) { 
       if(response.refreshStatus !=="REFRESH_COMPLETED"){ 
//stop getRefreshInfo infinite call once you get status REFRESH_COMPLETED 
clearInterval(refresh); 

       } 
       }, 
         error:function(request, status, error){ 

       } 
       }); 
       } 
      } 
     }); 
     return refreshView; 
    });`enter code here`