0

Ajax 호출의 성공 노드에서 위젯 함수를 호출하려고하는데, 성공하지 못했습니다.아약스 성공에서 JQuery 위젯 함수를 호출하는 방법?

내 앱을 사용하면 몇 가지 설명과 함께 일부 마커를 Google지도에 추가 할 수 있습니다. JQuery-addresspicker 위젯과 레일즈를 사용하고 있습니다. 마커를 추가하는 함수를 추가하고 설명 텍스트 영역이있는 양식을 표시하고 정보를 제출하는 버튼을 표시합니다. 따라서 사용자가 제출 한 후 앱은 사용자의 데이터를 저장하는 Ajax 함수를 호출하고 성공적으로 완료되면 다른 InfoWindow와 같은 다른 위젯 함수를 호출하려고한다.

문제는 성공한 Ajax 노드에서 다른 위젯 함수를 호출하는 방법을 모른다는 것입니다.

JQuery-addresspicker.js 
. 
. 
. 

_addFormListener: function(map, marker) { 
    var form = $(".add-form").clone().show(); 
    var infoWindowContent = form[0]; 
    var infoWindow = new google.maps.InfoWindow({ 
     content: infoWindowContent 
    }); 

    google.maps.event.addListener(marker, "click", function() { 
     infoWindow.open(map, this); 
    }); 

    form.submit(function (event){ 
     event.preventDefault(); 

     var description = $("textarea[name=description]", this).val(); 
     var latitude = marker.getPosition().lat(); 
     var longitude = marker.getPosition().lng(); 

     var data = { 
     description : description, 
     latitude  : latitude, 
     longitude  : longitude 
     }; 

     $.ajax({ 
     type : "POST", 
     url : "/places", 
     data: {place: data}, 
     beforeSend: function(x) { 
      x.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content')) 
     }, 
     success: function(x) { 
      this._closeFormFields(); // Not working! 
     } 
     }); 
    }); 
}, 

_cleanFormFields: function() { 
    console.log("testing"); 
} 
. 
. 

PlacesController 
def create 
@place = Place.new(params[:place]) 

if @place.save 
    redirect_to places_path 
else 
    render :status => 422 
end 

브라우저의 콘솔은 "catch되지 않은 형식 오류를 발생 : 개체 [개체 창]있는 방법 '_cleanFormFields'

어떤 아이디어 감사

+0

같은 위젯에 대한 참조를 저장하는 변수를 추가,이 문제를 해결합니다. – RadBrad

+0

답장을 보내 주셔서 감사합니다, RadBrab. 시도했지만 예외는 이제 다음과 같습니다. "Uncaught ReferenceError : _cleanFormField가 정의되지 않았습니다." – hugalves

+0

_addFormListener 메서드 앞에 _cleanFormFields를 정의하면 어떻게됩니까? –

답변

0

여기서 문제는 범위입니다이 없다?! this - ajax 호출이 을 덮어 씁니다.이은 ajax 호출 객체를 참조하기 때문에 위젯 참조가 느슨해집니다.

은 _closeFormFields 앞의 '이'제거

form.submit(function (event){ 
    event.preventDefault(); 

    var description = $("textarea[name=description]", this).val(); 
    var latitude = marker.getPosition().lat(); 
    var longitude = marker.getPosition().lng(); 

    var data = { 
    description : description, 
    latitude  : latitude, 
    longitude  : longitude 
    }; 

    //new widget reference var (this here still refers to the widget) 
    var widget = this; 

    $.ajax({ 
    type : "POST", 
    url : "/places", 
    data: {place: data}, 
    beforeSend: function(x) { 
     x.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content')) 
    }, 
    success: function(x) { 
     //now use the var widget here 
     widget._closeFormFields(); // Works! 
    } 
    }); 
}); 
관련 문제