2013-12-11 31 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`