0

参照元のバッキング Bean の値がである<p:inputText id="address">場合に検証メッセージを表示する必要があります。値は によって与えられ、この JS 関数にはかなりの遅延があります。<p:inputText id="validField">falsecodeAddress()

<h:form id="addressForm">
    <p:inputText id="address" onchange="codeAddress()"/>
    <p:message id="addressValidate" for="address" display="text"/>
    <p:commandButton value="submit" />
    <p:inputText id="validField" value="#{addressBean.valid}"/>
</h:form>

js:

var geocoder;
var map;
var valid = false;
var fullAddress = "none";

function initialize() {
    geocoder = new google.maps.Geocoder();
}

function codeAddress() {
    var address = (document.getElementById('addressForm:address').value + ", Germany");
    geocoder.geocode({'address' : address},function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latLong = results[0].geometry.location;
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            var country, postal_code, locality, street_number, route;
            for (i = 0; i < results[0].address_components.length; ++i) {
                var component = results[0].address_components[i];
                if (!locality && component.types.indexOf("locality") > -1)
                    locality = component.long_name;
                else if (!postal_code && component.types.indexOf("postal_code") > -1)
                    postal_code = component.long_name;
                else if (!country && component.types.indexOf("country") > -1)
                    country = component.long_name;
                else if (!street_number && component.types.indexOf("street_number") > -1)
                    street_number = component.long_name;
                else if (!route && component.types.indexOf("route") > -1)
                    route = component.long_name;
            }
            if (typeof latLong != "undefined"
                && typeof latitude != "undefined"
                && typeof longitude != "undefined"
                && typeof route != "undefined"
                && typeof street_number != "undefined"
                && typeof postal_code != "undefined"
                && typeof locality != "undefined"
                && typeof country != "undefined"){
                valid = true;               
                fullAddress = results[0].formatted_address;
            }
            else{
                valid=false;
                fullAddress="none";
            };
        }
        else{

            valid=false;
            fullAddress="none";
        }


    });
    window.setTimeout(sendAjaxical, 200);
}

function sendAjaxical(){
//  alert(fullAddress + valid);
    document.getElementById('addressForm:fullAddress').value = fullAddress;
    document.getElementById('addressForm:validField').value = valid;
    jsf.ajax.request(this, event, {execute:"@form"});
    if(valid){
        document.location.href='nextPage.xhtml';
    }else{
        showValidation();
    }
};

更新された js:

var geocoder;
var map;


function initialize() {
    geocoder = new google.maps.Geocoder();
}

function codeAddress() {
    var address = (document.getElementById('addressForm:address').value + ", Germany");
    geocoder.geocode({
        'address' : address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latLong = results[0].geometry.location;
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            var country, postal_code, locality, street_number, route;
            for (var i = 0; i < results[0].address_components.length; ++i) {
                var component = results[0].address_components[i];
                if (!locality && component.types.indexOf("locality") > -1)
                    locality = component.long_name;
                else if (!postal_code
                        && component.types.indexOf("postal_code") > -1)
                    postal_code = component.long_name;
                else if (!country && component.types.indexOf("country") > -1)
                    country = component.long_name;
                else if (!street_number
                        && component.types.indexOf("street_number") > -1)
                    street_number = component.long_name;
                else if (!route && component.types.indexOf("route") > -1)
                    route = component.long_name;
            }
            if (typeof latLong != "undefined" && typeof latitude != "undefined"
                    && typeof longitude != "undefined"
                    && typeof route != "undefined"
                    && typeof street_number != "undefined"
                    && typeof postal_code != "undefined"
                    && typeof locality != "undefined"
                    && typeof country != "undefined") {
                sendAjaxical(true,results[0].formatted_address);
            } else {
                sendAjaxical(false,"none");
            }
        } else {

            sendAjaxical(false,"none");
        }

    });
}

function sendAjaxical(valid,fullAddress) {
    // alert(fullAddress + valid);
    document.getElementById('addressForm:fullAddress').value = fullAddress;
    document.getElementById('addressForm:validField').value = valid;
    jsf.ajax.request(this, event, {
        execute : "@form"
    });
    if (valid) {
        document.location.href = 'nextPage.xhtml';
    }
}
4

1 に答える 1

0

任意のタイムアウトの後ではなく、ジオコーダーが終了したときに JSF ajax リクエストを呼び出す必要があります。

変化する

geocoder.geocode({'address' : address},function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        // ...
    }
});

window.setTimeout(sendAjaxical, 200);

geocoder.geocode({'address' : address},function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        // ...

        sendAjaxical();
    }
});
于 2013-02-14T13:08:27.617 に答える