BBC's guide to development
  • General

    • About
    • Tools
    • Git(hub)
    • Showpad
    • Hosting
    • Maintenance
    • Security
    • Go live checklist
  • Front-end development

    • Bundlers
    • CSS/SCSS
    • Javascript
    • Vue
    • PHP
    • Mails
    • Dev Faq
  • Functions
  • Mixins
  • General

    • OOP Structure
  • Component Classes

    • Accordion
    • App
    • Component
    • HighwayApp
    • Popup
    • PNG Sequencer
    • Tab
  • Manager Classes

    • BountListenerMgr
    • Cache
    • Configuration
    • InViewStateMgr
    • Instance Manager
    • Event dispatcher
  • Factories

    • SwiperFactory
  • PDF

    • AssetLoader
    • BasePdfDoc
    • TemplatePdfDoc
    • CustomPdfDoc
  • Utility functions

    • canvas
    • Connection Status
    • css
    • dev
    • placeholder
    • dom
    • fetch
    • json
    • object
    • scroll
    • scrollbar
    • spreadsheets
    • string
    • url
  • General

    • ComponentMgr
    • ThreeJsViewer
  • Components

    • ComponentMgr
    • GltfModel
    • Snappable
    • Socket
    • ThreeJsViewer
    • ThreeJsViewerCamera
  • Loaders

    • ConfigurationSerializer
    • GltfBlockParser
  • Utils

    • CanvasInputAdapter
    • CollisionManager
    • SocketGridExpander
    • blender
    • headless
  • General

    • Troubleshooting
    • Legacy
  • Components

    • AssetBar
    • ConfigGenerator
    • ShowpadApp
  • Managers

    • Assets
    • AppsDb
    • Config
  • Utils

    • Connection Status
    • general
    • showpad-interactive
    • showpad-upload
  • Components

    • Accordion
    • BackButton
    • Breadcrumb
    • ByltButton
    • Hamburger
    • Icon
    • Logo
    • Loader
    • Modal
    • Popup
    • Prompt
    • ProgressBar
    • TextLoader
  • Composables

    • useDebugMode
    • useConnectionStatus
  • Utils

    • dom
    • props
  • General

    • General
    • Tracking
  • Components

    • Accordion
    • ActionButton
    • AssetItem
    • AssetList
    • BackButton
    • ConfigGenButton
    • Logo
    • Media
    • Modal
    • Popup
    • Prompt
    • SPButton
    • SPRouterView
    • SPTrackedRouterLink
    • TextLoader
    • View
  • Composables

    • useConnectionStatus
  • Stores

    • useAppsDbStore
    • useBreadcrumbStore
    • useShowpadAPIStore
    • useShowpadSDKStore
    • useSpConfigStore
    • useSpStore
    • useSpTrackingStore
  • The New Kit

    • General
    • Installation & Usage
    • ACF Blocks
    • PHPCS
    • Functions
    • Vite
    • WP Config
    • Staging Deployment
  • Best Practices

    • Page Structure
    • Fonts/Typography
  • Todo
GitHub
  • General

    • About
    • Tools
    • Git(hub)
    • Showpad
    • Hosting
    • Maintenance
    • Security
    • Go live checklist
  • Front-end development

    • Bundlers
    • CSS/SCSS
    • Javascript
    • Vue
    • PHP
    • Mails
    • Dev Faq
  • Functions
  • Mixins
  • General

    • OOP Structure
  • Component Classes

    • Accordion
    • App
    • Component
    • HighwayApp
    • Popup
    • PNG Sequencer
    • Tab
  • Manager Classes

    • BountListenerMgr
    • Cache
    • Configuration
    • InViewStateMgr
    • Instance Manager
    • Event dispatcher
  • Factories

    • SwiperFactory
  • PDF

    • AssetLoader
    • BasePdfDoc
    • TemplatePdfDoc
    • CustomPdfDoc
  • Utility functions

    • canvas
    • Connection Status
    • css
    • dev
    • placeholder
    • dom
    • fetch
    • json
    • object
    • scroll
    • scrollbar
    • spreadsheets
    • string
    • url
  • General

    • ComponentMgr
    • ThreeJsViewer
  • Components

    • ComponentMgr
    • GltfModel
    • Snappable
    • Socket
    • ThreeJsViewer
    • ThreeJsViewerCamera
  • Loaders

    • ConfigurationSerializer
    • GltfBlockParser
  • Utils

    • CanvasInputAdapter
    • CollisionManager
    • SocketGridExpander
    • blender
    • headless
  • General

    • Troubleshooting
    • Legacy
  • Components

    • AssetBar
    • ConfigGenerator
    • ShowpadApp
  • Managers

    • Assets
    • AppsDb
    • Config
  • Utils

    • Connection Status
    • general
    • showpad-interactive
    • showpad-upload
  • Components

    • Accordion
    • BackButton
    • Breadcrumb
    • ByltButton
    • Hamburger
    • Icon
    • Logo
    • Loader
    • Modal
    • Popup
    • Prompt
    • ProgressBar
    • TextLoader
  • Composables

    • useDebugMode
    • useConnectionStatus
  • Utils

    • dom
    • props
  • General

    • General
    • Tracking
  • Components

    • Accordion
    • ActionButton
    • AssetItem
    • AssetList
    • BackButton
    • ConfigGenButton
    • Logo
    • Media
    • Modal
    • Popup
    • Prompt
    • SPButton
    • SPRouterView
    • SPTrackedRouterLink
    • TextLoader
    • View
  • Composables

    • useConnectionStatus
  • Stores

    • useAppsDbStore
    • useBreadcrumbStore
    • useShowpadAPIStore
    • useShowpadSDKStore
    • useSpConfigStore
    • useSpStore
    • useSpTrackingStore
  • The New Kit

    • General
    • Installation & Usage
    • ACF Blocks
    • PHPCS
    • Functions
    • Vite
    • WP Config
    • Staging Deployment
  • Best Practices

    • Page Structure
    • Fonts/Typography
  • Todo
GitHub
  • BoundListenerMgr

BoundListenerMgr

import BoundListenerMgr from '@bbc/front-end-kit/js/managers/BoundListenerMgr';

Sometimes it can be tough to keep access to the original scope when listeners are called by events. Luckilly there is a way to bind the scope to a function before it is executed.

The downside of this is when listeners need to be cleaned up to avoid double executions later. When binding a method, a new method is returned and passed to the subscribe methods like addEventListener or on. This means that the new method is the subscibed one and needs to be passed to the unsubscribe methods like removeEventListener or off and thus needs to be kept in memory and accessible untill the clean up process takes place.

This is where BoundListenerMgr comes in.

It will track all the bound methods in a single manager instance within the scope that it is operating while keeping the scope it is working in as clean as possible.

Getting started

class CustomClass {
    constructor () {

        // ...

        // initialise the manager
        this._blm = new BoundListenerMgr({
            scope: this
        });

        // add listeners to dom elements
        this._$button.addEventListener('click', this._blm.add('onButtonClick', onButtonClick));

        // ...

    }

    destroy() {
        // ...

        // remove listeners to dom elements
        this._$button.removeEventListener('click', this._blm.remove('onButtonClick'));

        // destroy the manager
        this._blm.destroy();

        // ...

    }
}

API

Methods

constructor(args = {})

The constructor method initialises the new BoundListenerMgr instance.

Parameters
  • args: The configuration object used by the constructor to initialise the BoundListenerMgr in the desired way.
    • scope: The scope it needs to bind to every function it manages

add (key, method, scopeOverride = null)

Adds an method to the list of the given key and binds it to either the scope of the instance or a given scope if provided while the add() method is called.

Parameters
  • key: Key on which the listener is stored so it can be retrieved later
  • method: Method to bind to the scope
  • scopeOverride: Way of overriding the scope the instance would use otherwise
Return

Returns the bound version of the method instantly so it can be passed all at once.

addOnce (key, method, scopeOverride = null)

Adds a method just like add() does but checks if the given key isn't already in use. If it is it will not bind the method and return the previously bound method instead.

Parameters
  • key: Key on which the method is stored so it can be retrieved later
  • method: Method to bind to the scope
  • scopeOverride: Way of overriding the scope the instance would use otherwise
Return

Returns the bound version of the method instantly so it can be passed all at once.

get (key)

Gets the bound method stored on the given key.

Parameters
  • key: The key on which the method is stored.
Return

Returns the bound method if found, if not it returns undefined

getDefinition (key)

Gets the full entry definition that is stored on the given key. The returned object contains all the information the instance uses to keep track of the listener in its map

Parameters
  • key: The key on which the definition is stored.
Return

Returns an object with the following parameters

  • originalMethod: The original method that has been bound to another scope
  • boundMethod: The newly created method wrapper that runs originalMethod in the right scope
  • scope: The scope that was used

remove (key)

Removes a bound method on the given key and returns the bound method after is done.

Parameters
  • key: The key on which the method is stored.
Return

Returns the bound method that was removed from the instance.

destroy ()

Destroys the instance and clears its internal map.

Methods (continued)

map ()

Returns the internal Map in which all the method, bound method and scope data is stored.

Not a getter

map is defined as a regular method in the source (map() { return this._map; }), not a getter. Call it as blm.map(), not blm.map.

Private Properties

_map

The internal Map in which all the method, bound method and scope data is stored.

_scope

The scope the instance will used by default when binding new methods.

Edit this page
Last Updated: 4/27/26, 12:56 PM
Contributors: Nicolas Jaenen