I am having trouble making the callback work correctly. Here is what I am trying to achieve: I have 2 items that I want to add to cart so I make 2 asynchronous POST requests. Once those two POST requests are complete, then I want to update the partial view of the cart. The issue is that it seems like only 1 item gets added to cart. When I debug it, then 2 of the items gets added. Any suggestion or help would be great. Thanks in advance!
here is my code:
var cart = []
function AddToCart(input) {
return $.ajax({
method: 'POST',
url: '/cart/add.js',
data: input,
dataType: 'json'
})
.done(function(data) {
return data;
});
}
$("#addToCart").click(function() {
$('#capsContainer input:checked').each(function() {
var cartItem = {
id: $(this).val(),
quantity: 1
}
cart.push(cartItem);
});
var test1 = AddToCart(cart[0]);
var test2 = AddToCart(cart[1]);
$.when.apply(test1, test2).done(function() {
$.getJSON('/cart.js', function(data) {
$('.cart-item-count').text(data.item_count);
$('.cart-item-price').html(formatMoney(data.total_price));
ShowCart();
});
})
});
2
Answers
Your AddToCart method already returns a deferred object. You are calling .done method twice.
I guess your code should look like this.
Part of the issue is that your using ajax requests which could occur either before or after the code happens to handle those executions. Since they are async, its possible that they could fire/return before any other code runs on the page depending on how your browser’s Javascript parser decides to execute the code. Rather than attempting to use Callbacks on asynchronous ajax requests, you can control the entire interaction by using a small 600 byte library called ajaxq.js
ajaxq.js essentially works just like jQuery’s
$.post()
method, only you can also specify multiple queues by name to execute asyncronously, and attach a callback to them or whatever.Here’s a quick example of how you could setup your Ajax Cart Preview using this library.
Here’s your example using the ajaxq.js library: