简体   繁体   English

如何绑定和取消绑定无限ajax滚动,以便其他jQuery工作?

[英]How do I bind and unbind infinite-ajax-scroll so other jQuery works?

Note: I know this is a common issue and a lot has been written about this but I can't seem to fix it. 注意:我知道这是一个常见的问题,对此已经写了很多文章,但是我似乎无法解决。

My Shopify store uses Ajax call's to add products to the cart and jQuery to update the front-end. 我的Shopify商店使用Ajax调用将产品添加到购物车,并使用jQuery更新前端。 I recently installed infinite-ajax-scroll but this brought some issues. 我最近安装了无限ajax-scroll,但这带来了一些问题。

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. 向下滚动到Infinite-ajax-scroll加载的产品,然后单击“添加到购物车”按钮时,ajax调用和jQuery更新不再起作用,它将我重定向到购物车页面。

Here's the inline script I'm using to activate IAS: 这是我用来激活IAS的内联脚本:

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

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

This is the code ( ajaxify.js line 161 - 194) that's responsible for adding the product to the cart: 这是负责将产品添加到购物车的代码( ajaxify.js第161-194行):

Shopify.addItemFromForm = function(form, callback, errorCallback) {
  // Unbind IAS
  ias.destroy();

  var params = {
    type: 'POST',
    url: '/cart/add.js',
    data: jQuery(form).serialize(),
    dataType: 'json',
    success: function(line_item) {
      if ((typeof callback) === 'function') {
        callback(line_item, form);
      }
      else {
        Shopify.onItemAdded(line_item, form);
      }
    },
    error: function(XMLHttpRequest, textStatus) {
      if ((typeof errorCallback) === 'function') {
        errorCallback(XMLHttpRequest, textStatus);
      }
      else {
        Shopify.onError(XMLHttpRequest, textStatus);
      }
    }
  };
  jQuery.ajax(params);

  var variant_id = params.data.split('=')[1]
  $( "#var-id-" + variant_id + " " + "#in-cart-indicator" ).removeClass( "not-in-cart" ).addClass( "triangle-top-right" );

  // Bind IAS
  ias.bind();
};

I've added the bind and unbind method based on this SO post , but without success. 我已经基于此SO post添加了bindunbind方法,但是没有成功。

What am I doing wrong? 我究竟做错了什么?

You can take a look at the page in question here . 您可以在此处查看相关页面。

I solved this by reinitialising ajaxifyShopify every time the new products finished rendering using infinite-ajax-scroll's rendered event. 每当新产品使用无限ajax-scroll的rendered事件完成渲染时,我都会通过重新初始化ajaxifyShopify来解决此问题。

Copy and paste the following code in collection.liquid : 将以下代码复制并粘贴到collection.liquid

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

ias.on('rendered', function() { 
  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
    });
  });
})
</script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM