我可以添加一个事件监听器来触发这个javascript类中的特定函数吗?
使用shopify应用程序时,需要使“添加到购物车”按钮不重定向到购物车页面,只需将项目添加到购物车即可。我发现了一个代码片段,它向Sopify cart发送POST请求,所以它可以工作。问题是应用程序有一个sidecart,并且它不会根据POST请求更新Quantity。我必须刷新网页的网站更新和显示真实数量。 但是,当您单击购物车中的“+”时,有适当的代码可以正确更新数量。我看到它发送一个GET请求,并相应地更新数据。如何使“添加到购物车”按钮调用该方法?我试着加了一个触发器,但没能让它工作。我在追求“重新招标”的方法。这是一个被称为内部。 我看到了“\uu attachListeners”方法,但无法让它将onclick侦听器附加到我的按钮:((尝试使方法执行console.log,也不起作用)我可以添加一个事件监听器来触发这个javascript类中的特定函数吗?,javascript,ajax,methods,event-handling,shopify,Javascript,Ajax,Methods,Event Handling,Shopify,使用shopify应用程序时,需要使“添加到购物车”按钮不重定向到购物车页面,只需将项目添加到购物车即可。我发现了一个代码片段,它向Sopify cart发送POST请求,所以它可以工作。问题是应用程序有一个sidecart,并且它不会根据POST请求更新Quantity。我必须刷新网页的网站更新和显示真实数量。 但是,当您单击购物车中的“+”时,有适当的代码可以正确更新数量。我看到它发送一个GET请求,并相应地更新数据。如何使“添加到购物车”按钮调用该方法?我试着加了一个触发器,但没能让它工作
_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', '.QuantitySelector__CurrentQuantity', 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'), 0.5, {
y: '100%',
transition: 'none',
ease: Cubic.easeInOut
}, 0);
}
} else {
content.text().then(function(html) {
_this37._replaceContent(html);
});
}
});
}
}