Skip to content
This repository has been archived by the owner on Aug 30, 2018. It is now read-only.

Prevent cart form submission while ajax running #438

Merged
merged 1 commit into from
Jun 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion assets/ajax-cart.js.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ var ajaxCart = (function(module, $) {
var init, loadCart;

// Private general variables
var settings, $body;
var settings, isUpdating, $body;

// Private plugin variables
var $formContainer, $addToCart, $cartCountSelector, $cartCostSelector, $cartContainer, $drawerContainer;
Expand Down Expand Up @@ -180,6 +180,9 @@ var ajaxCart = (function(module, $) {
// General Selectors
$body = $('body');

// Track cart activity status
isUpdating = false;

// Setup ajax quantity selectors on the any template if enableQtySelectors is true
if (settings.enableQtySelectors) {
qtySelectors();
Expand Down Expand Up @@ -362,6 +365,13 @@ var ajaxCart = (function(module, $) {
}
});

// Prevent cart from being submitted while quantities are changing
$body.on('submit', 'form.ajaxcart', function(evt) {
if (isUpdating) {
evt.preventDefault();
}
});

// Highlight the text when focused
$body.on('focus', '.ajaxcart__qty-adjust', function() {
var $el = $(this);
Expand All @@ -371,6 +381,8 @@ var ajaxCart = (function(module, $) {
});

function updateQuantity(line, qty) {
isUpdating = true;

// Add activity classes when changing cart quantities
var $row = $('.ajaxcart__row[data-line="' + line + '"]').addClass('is-loading');

Expand All @@ -394,6 +406,8 @@ var ajaxCart = (function(module, $) {
};

adjustCartCallback = function (cart) {
isUpdating = false;

// Update quantity and price
updateCountPrice(cart);

Expand Down
2 changes: 1 addition & 1 deletion snippets/ajax-cart-template.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{% endcomment %}
<script id="CartTemplate" type="text/template">
{% raw %}
<form action="/cart" method="post" novalidate class="cart">
<form action="/cart" method="post" novalidate class="cart ajaxcart">
<div class="ajaxcart__inner">
{{#items}}
<div class="ajaxcart__product">
Expand Down