Skip to content

Commit

Permalink
feat: add option to run a listener only once (in same syntax as addEv…
Browse files Browse the repository at this point in the history
…entListener)
  • Loading branch information
crhallberg committed Jan 29, 2024
1 parent f316c99 commit d7ba34f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 8 additions & 2 deletions themes/bootstrap3/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ var VuFind = (function VuFind() {
listeners[event] = [];
}

listeners[event].push(fn);
return () => unlisten(event, fn);
const listenFn = !once : fn : (...args) => {
fn(...args);
// Automatically remove a listener we only want to run once
unlisten(arguments.callee);
};

listeners[event].push(listenFn);
return () => unlisten(event, listenFn);
}

function emit(event, ...args) {
Expand Down
3 changes: 1 addition & 2 deletions themes/bootstrap3/js/lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,8 @@ VuFind.register('lightbox', function Lightbox() {
// onclose behavior
if ('string' === typeof $(form).data('lightboxOnclose')) {
VuFind.listen('lightbox.closed', function lightboxClosed() {
VuFind.unlisten('lightbox.closed', arguments.callee); // only once
VuFind.evalCallback($(form).data('lightboxOnclose'), null, form);
});
}, { once: true });
}
// Prevent multiple submission of submit button in lightbox
if (submit.closest(_modal).length > 0) {
Expand Down

0 comments on commit d7ba34f

Please sign in to comment.