Ajax를 통해 장바구니에 대한 데이터를 얻고 있습니다. - 내가 쇼핑 카트 의 줄에 라인을 얻을 - 옵션이 선택 객체 를 채우는 데에 대한 한 줄에 나는 DB에 체크를 할 -이 옵션은 하나의 같은 경우 :jquery - 각 반복마다 ajax 호출 수행
아이디어는 다음과 같다 내 라인에서 선택된다.
async 옵션을 false로 설정하면이 작업을 수행 할 수있다. 그러나 내가 여기서 읽었던 것처럼 이것은 선호되는 방법이 아닙니다. 내 아키텍처를 어떻게 조정해야합니까? 그 이후의 코드 중 일부는 done()
의 내부에서 일어나는 것들에 의존하기 때문에 무엇을 async = false
//get items from cart
$.ajax({
type: "POST",
url: "db/getImagesCart.php"
}).done(function(data) {
//populate carts ul
var items = [];
obj = $.parseJSON(data.trim());
$.each(obj, function(id, value) {
//list formats of this item
$.ajax({
type: "POST",
url: "db/getFormats.php",
async: false
}).done(function(data){
var select_format = "<select style='text-align: center;'>";
formats = $.parseJSON(data.trim());
$.each(formats, function(id, test){
//add select tot variable + select if the same
if(test.format_id == value.idformat){
select_format += "<option value=" + test.format_id + " SELECTED data-price=" + test.format_price + ">" + test.format_name + "</option>";
} else {
select_format += "<option value=" + test.format_id + " data-price=" + test.format_price + ">" + test.format_name + "</option>";
}
});
select_format += "</select>";
items.push('<tr id="row' + value.item_id + '"><td class="remove"><a href="#" class="delete" id="' + value.item_id + '"><img src="img/delete.png" height="25px" style="border: 0;" /></a></td><td class="image"><a class="fancybox" rel="group" href="' + value.path + '"><img src="' + value.path_thumb + '" style="max-width: 100px; height: 75px;" /></a></td><td class="formaat">' + select_format + '</td><td class="totalprice">€ ' + value.price + '</td></tr>');
});
});
$("tbody").html(items.join(''));
$(".fancybox").fancybox({
openEffect: 'elastic',
closeEffect: 'elastic',
nextEffect: 'fade',
prevEffect: 'fade'
});
$(".delete").click(function(){
var cart_item = $(this).attr("id");
var variables = 'action=2' + '&cart_item_id=' + cart_item;
$.ajax({
type: "POST",
url: "db/cart.php",
data: variables
}).done(function(data){
if($.browser.msie){
$('#row' + cart_item).find('td').fadeOut('slow', function(){
$('#row' + cart_item).find('td').parent().remove();
});
} else {
$('#row' + cart_item).fadeOut('slow', function(){
$('#row' + cart_item).find('td').parent().remove();
});
}
calc_full();
})
});
$(".formaat").change(function(){
//recalculate prices
$('.cart tr:gt(0)').each(function(){
var theprice = $(this).find('.formaat select :selected').attr("data-price");
$(this).find('.totalprice').html("€ " + theprice);
//save to db
var cart_item = $(this).attr('id').slice(3);
var formaat = $(this).find('.formaat select :selected').val();
var variables = 'action=3' + '&format=' + formaat + '&cart_item_id=' + cart_item;
$.post("db/cart.php", variables, function(data){
});
calc_full();
});
})
calc_full();
});
이 코드는 모두 관련이 있습니까? 그것을 단축 할 수 있습니까? – gdoron
각 반복마다 AJAX 호출을 보내지 않고 전체 JSON을 AJAX를 통해 전송하고 서버 측에서 정렬하여 적절하게 다시 보낼 수 있습니까? 따라서 하나의 호출이 발생합니까? – tymeJV
각 구문을 별도의 함수로 분해하고이 중첩이 아닌 데이터를 전달할 수 있습니다. 가독성과 유지 보수성 향상. –