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(); }, /**