From f9e7102265b391d782a29c7ddace50373d8fdb86 Mon Sep 17 00:00:00 2001 From: Nathan Day Date: Tue, 30 Jun 2020 22:25:28 +0100 Subject: [PATCH 1/2] Update Click Event Binding + Bind Click event observer to parent element + This way any additional anchor tags that are added after will also be observed + add href attribute selector to link selector to ensure that it exists --- .../web/js/product/list/ajax-filter.js | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/view/frontend/web/js/product/list/ajax-filter.js b/view/frontend/web/js/product/list/ajax-filter.js index c98504d..c434347 100644 --- a/view/frontend/web/js/product/list/ajax-filter.js +++ b/view/frontend/web/js/product/list/ajax-filter.js @@ -23,7 +23,7 @@ define([ */ options: { - links: 'a:not([data-ajax=false])', // all links except those configured + links: 'a:not([data-ajax=false])[href]', // all links except those configured // with a 'data-ajax="false"' attribute contentContainer: '.column.main', sidebarMainContainer: '.sidebar-main', @@ -40,7 +40,7 @@ define([ * @return void */ _create: function () { - this._bind($(this.element).find(this.options.links)); + this._bind($(this.element)); }, /** @@ -50,9 +50,7 @@ define([ * @return void */ _bind: function (element) { - if (element.is('a')) { - element.on('click', $.proxy(this.processLink, this)); - } + element.on('click', $.proxy(this.processLink, this)); }, /** @@ -62,10 +60,12 @@ define([ * @return void */ processLink: function (event) { - event.preventDefault(); - this.ajaxSubmit($(event.currentTarget).attr('href')); + if ($(event.target).closest('a').is(this.options.links)) { + event.preventDefault(); + this.ajaxSubmit($(event.target).closest('a').attr('href')); + } }, - + /** * Update content and init all components defined via the data-mage-init * attribute @@ -74,16 +74,16 @@ define([ * @return void */ updateContent: function (html) { - if (html.content) { + if (html.content) { // Remove all products wrappers except the first one $(this.options.contentContainer).slice(1).remove(); - + // Update content $(this.options.contentContainer) .html(html.content) .trigger('contentUpdated'); } - + if (html.sidebar_main) { $(this.options.sidebarMainContainer) .empty() @@ -91,7 +91,7 @@ define([ .trigger('contentUpdated'); } }, - + /** * Replace the browser URL * @@ -107,7 +107,7 @@ define([ history.replaceState(null, null, url); } }, - + /** * Scroll to the top of the content container and update content * @@ -137,7 +137,7 @@ define([ */ ajaxSubmit: function (url) { var self = this; - + $.ajax({ url: url, data: 'ajax=1', From b326767f8138d71181f9711bfe155a9cf1138526 Mon Sep 17 00:00:00 2001 From: Nathan Day Date: Tue, 30 Jun 2020 22:28:01 +0100 Subject: [PATCH 2/2] Apply Bindings after Ajax Content Update + Addply Bindings after content is updated with Ajax response + If any KO templates were included in the response content + then the bindings need to be applied --- view/frontend/web/js/product/list/ajax-filter.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/view/frontend/web/js/product/list/ajax-filter.js b/view/frontend/web/js/product/list/ajax-filter.js index c434347..7fdeef0 100644 --- a/view/frontend/web/js/product/list/ajax-filter.js +++ b/view/frontend/web/js/product/list/ajax-filter.js @@ -81,14 +81,18 @@ define([ // Update content $(this.options.contentContainer) .html(html.content) - .trigger('contentUpdated'); + .trigger('contentUpdated') + .children() + .applyBindings(); } if (html.sidebar_main) { $(this.options.sidebarMainContainer) .empty() .html(html.sidebar_main) - .trigger('contentUpdated'); + .trigger('contentUpdated') + .children() + .applyBindings(); } },