2012-11-03 5 views
0

에서 나는 사용자 정의 필드를 사용하고이 값을 반환하고 어떻게 든이 때문에 내부 함수의 부울 값을 반환하는 우편 번호 필드의 유효성을 검사 할 필요가 없습니다 검증 :페이스 북 등록 플러그인 + 내 페이스 북 등록 플러그인의 기능

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

calculateCoordinates 비동기 함수 인 geocode를 사용한다. 따라서 내부 비동기 함수에서 모든 데이터를 '반환'하기 위해서는 비동기 적으로 사용해야합니다. 이것은 콜백 패턴을 사용하여 수행됩니다.

불행히도 동 기적으로 사용하고 있으며 등록 플러그인은 validate도 동 기적으로 사용해야합니다. 본질적으로이 작업을 할 수있는 방법이 없습니다.