From 5939b5dada9f44f854d11dbe31ebef313349096a Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Sat, 19 Oct 2024 18:50:54 -0700 Subject: [PATCH] fix: sometimes components could not be added --- .../js/views/modals/select_v2_library_content.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cms/static/js/views/modals/select_v2_library_content.js b/cms/static/js/views/modals/select_v2_library_content.js index 363ab06fcd3d..87523679678c 100644 --- a/cms/static/js/views/modals/select_v2_library_content.js +++ b/cms/static/js/views/modals/select_v2_library_content.js @@ -20,7 +20,7 @@ function($, _, gettext, BaseModal) { initialize: function() { BaseModal.prototype.initialize.call(this); // Add event listen to close picker when the iframe tells us to - window.addEventListener("message", function (event) { + const handleMessage = (event) => { if (event.data?.type === 'pickerComponentSelected') { var requestData = { library_content_key: event.data.usageKey, @@ -29,7 +29,14 @@ function($, _, gettext, BaseModal) { this.callback(requestData); this.hide(); } - }.bind(this), { once: true }, false); + }; + this.messageListener = window.addEventListener("message", handleMessage); + this.cleanupListener = () => { window.removeEventListener("message", handleMessage) }; + }, + + hide: function() { + BaseModal.prototype.hide.call(this); + this.cleanupListener(); }, /**