2012-08-14 48 views
0

我正在执行异步AJAX请求正在wrapped功能。其中$ .ajax是延迟对象,我可以正确使用.promise(检查:初始加载),那么我将无法做到'现在真正加载',这将在ajax完成加载之前执行。包装在功能异步AJAX请求将无法承诺()

function WSCall(method, data, callback, type, async, bg) { 
    // .. code .. 
    var promise = $.ajax({ 
     'url': useSampleData ? useSampleData || null, 
     //'async': false, 
     'type': 'POST', 
     'dataType': (type == null) ? 'json' : type, 
     'data': data, 
     'beforeSend': bg ? null : LoadingBegin, 
     'complete': bg ? null : LoadingEnd, 
     'success': callback, 
     'error' : bg ? null : function(jqXHR, textStatus, errorThrown) { networkError = 1; } 
    }); 

    promise.done(function(){ console.log('Initially loaded') }); 
} 

function aSyncEvent() { 
    WSCall(
     'status', 
     {}, 
     function (data) { 
      if (data.error) { 
       console.log('Error occured'); return ShowDialogAlert(data.error); } 
      if (data.statusResult) { 
       var parts = data.statusResult.split('-'); 
       if (parts[1] === '0') { 
        sId = parts[0]; 
        console.log('Wow its loaded!'); 
        return true; 
       }    
      } 
     } 
    ) 
} 

$.when(aSyncEvent()).then(function() { console.log('now really loaded')}); 

初始加载和哇后阿贾克斯依次但是“现在确实装”将AJAX执行完毕之前出现被执行其加载会正确显示。

我恳求关于此事的帮助。

感谢 迈克

+0

从[了'$。当()'DOCO](http://api.jquery.com/jquery.when/):_“如果一个参数是传递给'jQuery.when'并且它不是Deferred,它将被视为已解析的Deferred,并且将立即执行任何doneCallbacks。“_ – nnnnnn 2012-08-14 11:39:40

+0

感谢nnnnnn对于这些信息,我仍然想知道promise()如何检查如果Dererred得到解决。如果(真)不是关于平原的话? – Mike 2012-08-14 11:50:40

回答

0

你试过退回推迟?

function WSCall(method, data, callback, type, async, bg) { 
    // .. code .. 
    return promise.done(function(){ console.log('Initially loaded') }); 
} 

function aSyncEvent() { 
    return WSCall(
     // .. code .. 
    ); 
} 
+0

你是我的上帝。 – Mike 2012-08-14 11:48:31