2012-11-05 90 views
2

我在客戶端使用javascript和服務器端的node.js,實際上我試圖對輸入字段進行遠程驗證,結果必須是布爾值,它應該基於服務器調用,我的代碼如下,如何將異步調用轉換爲JavaScript中的同步?

jQuery.validator.addMethod("remoteValidation", function(value, element) { 

    var accountName = $("#accountName").val(); 
    var accountType = $("#accountType").val(); 
    var valid = this.Validation(accountType,accountName); 
    //returning undefined becoz return statement executing before server side execution 
    return valid; 

}, "Entered email or username already exists"); 

this.validation = function(accountType,accountName,cb){ 
    var valid =null; 
    ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,function(res) { 
      // res will return boolean 
     valid = res; 
    }); 
      //valid is null becoz this line is executing before valid = res line. 
    return valid; 
}; 

我的服務器端代碼:

services.imAccountsService.getAllImAccounts(accountType,accountName, 
    function(err,result){ 
    if(err){ 
    return; 
    } 
    if(result == null){ 
     res(true); 
    } 
    else{ 
     res(false); 
    } 

    }); 

我的問題是異步執行,如何使代碼synschronous在這種情況下..

+0

什麼是'ss'?如果有該方法的文檔,您應該考慮查看,因爲它是異步方法。 –

+0

@Quintin ss.rpc是一種異步方法。它是節點js – user2587222

+0

套接字流是否包含在默認的node.js包中?如果是這樣,我不知道。 –

回答

0

你將不得不等到的值res可用。看看下面的代碼是否有效。

jQuery.validator.addMethod("remoteValidation", function(value, element) { 

     var accountName = $("#accountName").val(); 
     var accountType = $("#accountType").val(); 
     var valid = this.Validation(accountType,accountName); 
     //returning undefined becoz return statement executing before server side execution 
     return valid; 

    }, "Entered email or username already exists"); 

    this.validation = function(accountType,accountName,cb){ 
     var valid =null; 
     ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,function(res) { 
       // res will return boolean 

      while(res <> null) 
      { 
       window.setInterval(function(){waitFunc()}, 1000); 
      } 
      valid = res; 
     }); 
       //valid is null becoz this line is executing before valid = res line. 
     return valid; 
    }; 

function waitFunc() 
{ 
//do nothing 
} 
+0

謝謝,但ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,函數(res)本身的異步代碼.. – user2587222

+0

多數民衆贊成在右..但是當它執行** res **會有一些價值,是那你的期望是什麼? –

+0

是的,你是對的,但是,ss.rpc回調沒有運行befre返回得到執行,如果我刪除返回ss.rpc回調正在運行我已經添加了服務器端代碼,PLZ看看那... – user2587222

1

在jquery驗證方法中增加了函數。

if(param.type === "rpc"){ 
      var args = param.data.remoteArgs(); 
      ss.rpc(param.url, args, function(response) { 
       validator.settings.messages[element.name].remote = previous.originalMessage; 
       var valid = response === true; 
       if (valid) { 
        var submitted = validator.formSubmitted; 
        validator.prepareElement(element); 
        validator.formSubmitted = submitted; 
        validator.successList.push(element); 
        validator.showErrors(); 
       } else { 
        var errors = {}; 
        var message = response || validator.defaultMessage(element, "remote"); 
        errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message; 
        validator.showErrors(errors); 
       } 
       previous.valid = valid; 
       validator.stopRequest(element, valid); 
      }); 
     } 

它適用於所有罰款....感謝...

相關問題