我可以添加一个事件监听器来触发这个 javascript 类中的特定函数吗?

如何解决我可以添加一个事件监听器来触发这个 javascript 类中的特定函数吗?

在 shopify 应用程序上工作,需要使“添加到购物车”按钮不重定向到购物车页面,而只需将项目添加到购物车。我找到了一个向 Sopify 购物车发送 POST 请求的代码片段,所以它可以工作。问题是应用程序有一个 sidecart,它不会更新 POST 请求的数量。我必须刷新网站页面才能更新并显示真实数量。 但是,当您单击购物车中的“+”时,有适当的代码可以正确更新数量。我看到它发送了一个 GET 请求,并相应地更新数据。如何让“添加到购物车”按钮调用方法?我尝试添加触发器,但无法使其正常工作。我追求的是“rerenderCart”方法。这是内部调用的那个。 我看到了“__attachListeners”方法,但无法让它将 onclick 侦听器附加到我的按钮 :((试图使方法执行 console.log,但也没有用)

_createClass(CartSection,[{
        key: 'onUnload',value: function onUnload() {
          if (this.options['hasShippingEstimator']) {
            this.shippingEstimator.destroy();
          }

          this.delegateElement.off();
          document.removeEventListener('product:added',this._onProductAddedListener);
        }
      },{
        key: '_attachListeners',value: function _attachListeners() {
          this._onProductAddedListener = this._onProductAdded.bind(this);

          this.delegateElement.on('change','#cart-note',this._updateCartNote.bind(this));

          if (this.options['type'] !== 'page') {
            this.delegateElement.on('click','[data-action="update-item-quantity"],[data-action="remove-item"]',this._updateItemQuantity.bind(this));
            this.delegateElement.on('change','.QuantitySelector__CurrentQuantity',this._updateItemQuantity.bind(this));
          } else {
            this.delegateElement.on('change',this._reloadPageWithQuantity.bind(this));
          }

          // We have some listeners that are specific to the fact it's a drawer or the dedicated cart page
          if (this.options['drawer']) {
            this.delegateElement.on('click','[data-action="toggle-cart-note"]',this._toggleCartNote.bind(this));
          }

          document.addEventListener('product:added',this._onProductAddedListener);

          // We attach a listener at page level which allows to re-render the cart
          this.documentDelegate.on('cart:refresh',this._rerenderCart.bind(this,false));
        }
      },{

        key: '_reloadPageWithQuantity',value: function _reloadPageWithQuantity(event,target) {
          window.location.href = window.routes.cartChangeUrl + '?quantity=' + parseInt(target.value) + '&line=' + target.getAttribute('data-line');
        }
      },{
        key: '_onProductAdded',value: function _onProductAdded(event) {
          var _this36 = this;

          this.itemCount += event.detail.quantity;

          this._rerenderCart().then(function() {
            _this36.sidebarDrawer.open();
          });
        }
      },{
        key: '_onDrawerClosed',value: function _onDrawerClosed() {
          if (this.isCartNoteOpen) {
            this._toggleCartNote();
          }
        }

        /**
         * This method is called internally to rerender the cart,based on the content returned by Shopify Ajax API.
         * We Could save some performance by updating directly in JavaScript instead of doing a GET call to get the HTML
         * from Shopify,but by experience,this allows for easier app integration as it allows the Liquid to re-run
         * all the time and hence having easier logic.
         */

      },{
        key: '_rerenderCart',value: function _rerenderCart(elementToAnimate) {
          var _this37 = this;

          // Note: appending a timestamp is necessary as the polyfill on IE11 and lower does not support the "cache" property
          return fetch(window.routes.cartUrl + '?view=' + (this.options['drawer'] && window.theme.pageType !== 'cart' ? 'drawer' : 'ajax') + '&timestamp=' + Date.Now(),{
            credentials: 'same-origin',method: 'GET'
          }).then(function(content) {
            // If there is an element to animate,we animate it using a transition
            if (_this37.options['drawer'] && elementToAnimate) {
              var timelineLite = new TimelineLite({
                onComplete: function onComplete() {
                  content.text().then(function(html) {
                    _this37._replaceContent(html);
                  });
                }
              });

              timelineLite.to(elementToAnimate,0.5,{
                height: 0,opacity: 0,ease: Cubic.eaSEOut
              },0);

              if (_this37.itemCount === 0) {
                timelineLite.to(_this37.element.querySelector('.Drawer__Footer'),{
                  y: '100%',transition: 'none',ease: Cubic.easeInOut
                },0);
              }
            } else {
              content.text().then(function(html) {
                _this37._replaceContent(html);
              });
            }
          });
        }
      }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?