如何解决我可以添加一个事件监听器来触发这个 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') + '×tamp=' + 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 举报,一经查实,本站将立刻删除。