Search code examples
javascriptjqueryajaxshopifyjquery-ias

How do I unbind this piece of jquery?


My Shopify store uses Ajax call's to add products to the cart and jQuery to update the front-end. I recently installed infinite-ajax-scroll but this brought some issues.

When scrolling down to the products loaded by infinite-ajax-scroll, and then click on the add to cart button, the ajax calls & jQuery updates don't work anymore, it redirects me to the cart page.

I solved this by reinitialising the piece of jQuery code that "ajaxifies" the shop using infinite-ajax-scroll's rendered event.

But now when scrolling down to the 20 new products loaded by infinite-ajax-scroll, ajaxifyShopify get's initialized for the second time on the first 20 products. When adding one of the first 20 products to the cart, they get added twice.

I tried unbinding the first ajaxifyShopify with jQuery's .off() but it doesn't work.

The complete code for ajaxifyShopify can be found on line 261 here. Every time a new page loads, ajaxifyShopify get's initialized to "ajaxify" the page.

Here's my code:

jQuery(function($) {
  ajaxifyShopify.init({
    method: '{{ settings.ajax_cart_method }}',
    wrapperClass: 'wrapper',
    formSelector: '#addToCartForm',
    addToCartSelector: '#addToCart',
    cartCountSelector: '#cartCount',
    toggleCartButton: '.cart-toggle',
    useCartTemplate: true,
    btnClass: 'btn',
    moneyFormat: {{ shop.money_format | json }},
    disableAjaxCart: false,
    enableQtySelectors: true
  });
});

console.log('ajaxifyShopify initialized for the first time');

var ias = jQuery.ias({
  container:  '#products',
  item:       '.single-product',
  pagination: '.pagination-custom',
  next:       '.next'
});

ias.extension(new IASSpinnerExtension({
  src: '{{ "spiffygif_36x36.gif" | asset_url }}'
}));

ias.on('rendered', function(data, items) {
  console.log('ias rendered');

  // Unbind ajaxifyShopify
  $("html").off("ajaxifyShopify");
  console.log('ajaxifyShopify off');

  // Initialize ajaxifyShopify
  jQuery(function($) {
    ajaxifyShopify.init({
      method: '{{ settings.ajax_cart_method }}',
      wrapperClass: 'wrapper',
      formSelector: '#addToCartForm',
      addToCartSelector: '#addToCart',
      cartCountSelector: '#cartCount',
      toggleCartButton: '.cart-toggle',
      useCartTemplate: true,
      btnClass: 'btn',
      moneyFormat: {{ shop.money_format | json }},
      disableAjaxCart: false,
      enableQtySelectors: true
    });

    console.log('ajaxifyShopify initialized from ias');
  });
})

You can take a look at the page in question here.

What am I doing wrong?


Solution

  • I solved this by unbinding each individual event handler binded by ajaxifyShopify.

    For those interested, here's the code:

    <script>
      jQuery(function($) {
        ajaxifyShopify.init({
          method: '{{ settings.ajax_cart_method }}',
          wrapperClass: 'wrapper',
          formSelector: '#addToCartForm',
          addToCartSelector: '#addToCart',
          cartCountSelector: '#cartCount',
          toggleCartButton: '.cart-toggle',
          useCartTemplate: true,
          btnClass: 'btn',
          moneyFormat: {{ shop.money_format | json }},
          disableAjaxCart: false,
          enableQtySelectors: true
        });
      });
    
      console.log('ajaxifyShopify initialized for the first time');
    
      var ias = jQuery.ias({
        container:  '#products',
        item:       '.single-product',
        pagination: '.pagination-custom',
        next:       '.next'
      });
    
      ias.extension(new IASSpinnerExtension({
        src: '{{ "spiffygif_36x36.gif" | asset_url }}'
      }));
    
      ias.on('rendered', function(data, items) {
        console.log('ias rendered event');
    
        // Unbind previous ajaxifyShopify event handlers
        $("wrapper").off();
        $("#addToCartForm").off();
        $("#addToCart").off();
        $("#cartCount").off();
        $(".cart-toggle").off();
        console.log('ajaxifyShopify off from ias rendered event');
    
        // Initialize ajaxifyShopify
        jQuery(function($) {
            ajaxifyShopify.init({
              method: '{{ settings.ajax_cart_method }}',
              wrapperClass: 'wrapper',
              formSelector: '#addToCartForm',
              addToCartSelector: '#addToCart',
              cartCountSelector: '#cartCount',
              toggleCartButton: '.cart-toggle',
              useCartTemplate: true,
              btnClass: 'btn',
              moneyFormat: {{ shop.money_format | json }},
              disableAjaxCart: false,
              enableQtySelectors: true
            });
    
          console.log('ajaxifyShopify initialized from ias rendered event');
        });
      })
      </script>