Skip to content

Commit 9e80acf

Browse files
committed
added missing jquery
1 parent aa1763d commit 9e80acf

File tree

3 files changed

+105
-67
lines changed

3 files changed

+105
-67
lines changed

dist/breinify-shopify.js

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,8 @@
5151
}
5252

5353
if (config.captureCartFetchEvents === true) {
54-
this.originalFetch = window.fetch;
55-
56-
window.fetch = function () {
57-
const args = arguments;
58-
const url = args[0];
59-
60-
if (typeof url !== 'string' || !url.startsWith('/cart/')) {
61-
return _self.originalFetch.apply(this, args);
62-
}
63-
64-
return _self.originalFetch.apply(this, args).then(response => {
65-
// response.clone().json().then(data => _self._loadCart());
66-
67-
_self._loadCart();
68-
return response;
69-
});
70-
};
71-
72-
this.originalAjax = window.jQuery.ajax;
73-
window.jQuery.ajax = function (settings) {
74-
75-
// settings can be a string (url) or an object
76-
const url = typeof settings === 'string' ? settings : settings.url;
77-
78-
if (typeof url !== 'string' || !url.startsWith('/cart/')) {
79-
return _self.originalAjax.apply(this, arguments);
80-
}
81-
82-
// Call original ajax, then trigger your cart update logic on success
83-
return _self.originalAjax.call(this, settings).done(function () {
84-
_self._loadCart();
85-
});
86-
};
54+
this._bindFetch();
55+
this._bindJQuery();
8756
}
8857

8958
if ($.isArray(config.additionalData)) {
@@ -96,6 +65,56 @@
9665
this.isSetup = true;
9766
},
9867

68+
_bindJQuery: function(callback, waited) {
69+
const _self = this;
70+
71+
if (!$.isFunction(window.jQuery) || !$.isFunction(window.jQuery.ajax)) {
72+
waited = waited || 0;
73+
setTimeout(function() {
74+
_self._bindJQuery(callback, waited + 50);
75+
}, 50);
76+
77+
return;
78+
}
79+
80+
this.originalAjax = window.jQuery.ajax;
81+
window.jQuery.ajax = function (settings) {
82+
83+
// settings can be a string (url) or an object
84+
const url = typeof settings === 'string' ? settings : settings.url;
85+
86+
if (typeof url !== 'string' || !url.startsWith('/cart/')) {
87+
return _self.originalAjax.apply(this, arguments);
88+
}
89+
90+
// Call original ajax, then trigger your cart update logic on success
91+
return _self.originalAjax.call(this, settings).done(function () {
92+
_self._loadCart();
93+
});
94+
};
95+
},
96+
97+
_bindFetch: function() {
98+
const _self = this;
99+
100+
this.originalFetch = window.fetch;
101+
window.fetch = function () {
102+
const args = arguments;
103+
const url = args[0];
104+
105+
if (typeof url !== 'string' || !url.startsWith('/cart/')) {
106+
return _self.originalFetch.apply(this, args);
107+
}
108+
109+
return _self.originalFetch.apply(this, args).then(response => {
110+
// response.clone().json().then(data => _self._loadCart());
111+
112+
_self._loadCart();
113+
return response;
114+
});
115+
};
116+
},
117+
99118
getToken: function () {
100119
let token = $.isFunction(this.lookUpToken) ? this.lookUpToken() : null;
101120
if (typeof token !== 'string' || token.trim() === '') {

dist/breinify-shopify.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/Shopify.js

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,8 @@
5151
}
5252

5353
if (config.captureCartFetchEvents === true) {
54-
this.originalFetch = window.fetch;
55-
56-
window.fetch = function () {
57-
const args = arguments;
58-
const url = args[0];
59-
60-
if (typeof url !== 'string' || !url.startsWith('/cart/')) {
61-
return _self.originalFetch.apply(this, args);
62-
}
63-
64-
return _self.originalFetch.apply(this, args).then(response => {
65-
// response.clone().json().then(data => _self._loadCart());
66-
67-
_self._loadCart();
68-
return response;
69-
});
70-
};
71-
72-
this.originalAjax = window.jQuery.ajax;
73-
window.jQuery.ajax = function (settings) {
74-
75-
// settings can be a string (url) or an object
76-
const url = typeof settings === 'string' ? settings : settings.url;
77-
78-
if (typeof url !== 'string' || !url.startsWith('/cart/')) {
79-
return _self.originalAjax.apply(this, arguments);
80-
}
81-
82-
// Call original ajax, then trigger your cart update logic on success
83-
return _self.originalAjax.call(this, settings).done(function () {
84-
_self._loadCart();
85-
});
86-
};
54+
this._bindFetch();
55+
this._bindJQuery();
8756
}
8857

8958
if ($.isArray(config.additionalData)) {
@@ -96,6 +65,56 @@
9665
this.isSetup = true;
9766
},
9867

68+
_bindJQuery: function(callback, waited) {
69+
const _self = this;
70+
71+
if (!$.isFunction(window.jQuery) || !$.isFunction(window.jQuery.ajax)) {
72+
waited = waited || 0;
73+
setTimeout(function() {
74+
_self._bindJQuery(callback, waited + 50);
75+
}, 50);
76+
77+
return;
78+
}
79+
80+
this.originalAjax = window.jQuery.ajax;
81+
window.jQuery.ajax = function (settings) {
82+
83+
// settings can be a string (url) or an object
84+
const url = typeof settings === 'string' ? settings : settings.url;
85+
86+
if (typeof url !== 'string' || !url.startsWith('/cart/')) {
87+
return _self.originalAjax.apply(this, arguments);
88+
}
89+
90+
// Call original ajax, then trigger your cart update logic on success
91+
return _self.originalAjax.call(this, settings).done(function () {
92+
_self._loadCart();
93+
});
94+
};
95+
},
96+
97+
_bindFetch: function() {
98+
const _self = this;
99+
100+
this.originalFetch = window.fetch;
101+
window.fetch = function () {
102+
const args = arguments;
103+
const url = args[0];
104+
105+
if (typeof url !== 'string' || !url.startsWith('/cart/')) {
106+
return _self.originalFetch.apply(this, args);
107+
}
108+
109+
return _self.originalFetch.apply(this, args).then(response => {
110+
// response.clone().json().then(data => _self._loadCart());
111+
112+
_self._loadCart();
113+
return response;
114+
});
115+
};
116+
},
117+
99118
getToken: function () {
100119
let token = $.isFunction(this.lookUpToken) ? this.lookUpToken() : null;
101120
if (typeof token !== 'string' || token.trim() === '') {

0 commit comments

Comments
 (0)