단순화 내 Jquery xajax 같은 기능
-
06-07-2019 - |
문제
기본적으로 하려고 했던 복제하는 것 중 하나는 xajax 준 Jquery->을 정의하는 기능이 무엇이 내 응답을 수정하는 서버 측의 설정하지 않고도 클라이언트 측.으로 완벽하게 작동합니다,하지만 각 시간을 내가 원하는 전화를 다른 Jquery 기능을 추가하면 문이 있습니다.
각 요소에서 응답을 배열은 무언가를 포함하여 변경할 클라이언트 측.
나를 취할 수 있는 거의 모든이다.action=='blah'는 경우에 문으로 대체 어떤 영리한 자바 스크립트,하지만 분명히 나지 않은 영리한 충분합니다.:)
$.고 showmessage 은 단순히 교체에 대한 경고,제한 시간과 끈적 끈적한 비트입니다.
나의 클라이언트 Jquery 기능:
$.doAjax = function(func,values) {
$.ajax({ data: values, url: '/time/ajax/' + func, type: 'POST', dataType: 'json', timeout: 5000,
error: function(xhr, ajaxOptions,thrownError){ $.showMessage('Error processing request: ' + func,10000);$('#debug').html(xhr.responseText); },
success: function(data){
if(data){
$.each(data, function(){
if ( this.action == 'attr' ) $(this.target).attr(this.content);
if ( this.action == 'removeAttr' ) $(this.target).removeAttr(this.content);
if ( this.action == 'addClass' ) $(this.target).addClass(this.content);
if ( this.action == 'removeClass' ) $(this.target).removeClass(this.content);
if ( this.action == 'html' ) $(this.target).html(this.content);
if ( this.action == 'text' ) $(this.target).text(this.content);
if ( this.action == 'val' ) $(this.target).val(this.content);
if ( this.action == 'eval' ) {
try {
eval(this.content);
}
catch(err) {
$.showMessage(err,5000);
};
};
});
}
}
});
return this;
};
서버 측 코드:
header("Content-type: text/plain");
$response = array();
$response[] = array('action'=>'html','target'=>'#booked_time_group_unbilled','content'=>get_booked_time_group_unbilled());
$response[] = array('action'=>'html','target'=>'#booked_time_my_unbilled','content'=>get_booked_time_my_unbilled());
$response[] = array('action'=>'eval','target'=>'','content'=>"$.showMessage('The selected time has been deleted',5000,true)");
echo json_encode($response);
해결책
이동:
$(this.target)[this.action](this.content);
다른 팁
나는 정확하게 확신을 묻는?문체로,I would write it like this:
var handlers = {
attr: function() { $(this.target).attr(this.content) },
removeAttr: function() { $(this.target).removeAttr(this.content) },
// etc.
};
$.each(data,function() {
handlers[this.action].call(this);
});
는 경우에 당신을 실행하려면 어떤 서버로 보내고,다시는 단지 항상이 다시 보내는 eval action?
제휴하지 않습니다 StackOverflow