2012-11-03 18 views
0

我使用的是自定義字段在我的Facebook註冊的插件的返回值和需要驗證返回不知何故,因爲這種內在功能的一個布爾值,郵編場:Facebook的註冊插件+驗證無法從功能

geocoder.geocode({ address: address }, function (results, status) { 
} 

我無法將值返回給我的addExist變量,它總是返回'未定義'。誰能幫忙?謝謝。

<fb:registration redirect_uri="http://localhost:40153/Account/RegisterDestination.aspx" 
    fields='[ 
     {"name":"name"}, 
     {"name":"gender"}, 
     {"name":"email"}, 
     {"name":"birthday"}, 
     {"name":"password"}, 
     {"name":"zip","description":"Postal Code","type":"text"}]' 
     onvalidate="validate"></fb:registration> 

     function validate(form) { 
      errors = {}; 
      if (form.zip !== "") { 

       var addExist = calculateCoordinates(form.zip.toString()); 
       // alert(addExist); 
       if (!addExist) { 
        errors.zip = "Cannot locate address"; 
       } 
      } 
      return errors; 
     } 

     function calculateCoordinates(zip) { 

      var txtPostcode = zip; 
      // var address = txtAddress1.value + ', '; 
      // address += txtAddress2.value + ', '; 
      // address += txtTown.value + ', '; 
      var address = txtPostcode; 
      // address += txtCountry.value; 

      var geocoder; 
      geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({ address: address }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        var location = results[0].geometry.location; 
        var litLatClientId = document.getElementById("<%=litLat %>"); 
        var litLongClientId = document.getElementById("<%=litLong %>"); 

        $("#" + litLatClientId).val(location.lat()); 
        $("#" + litLongClientId).val(location.lng()); 

        // Successfully reach here but the bool variable 'addExist' value gets an 'Undefined' value. 

        return true; 
       } 
       else 
       return false; 
      }); 
     } 

回答

0

​​使用geocode,這是一個異步函數。因此,它必須自己異步使用,以便從內部異步函數'返回'任何數據。這是通過使用回調模式完成的。

不幸的是,你正在同步使用它,註冊插件也期望validate是同步的。因此基本上沒有辦法做這項工作。