Page Contents

Home > @loopback/context > Binding

Binding class

Binding represents an entry in the Context. Each binding has a key and a corresponding value getter.

Signature:

export declare class Binding<T = BoundValue> extends EventEmitter 

Extends: EventEmitter

Constructors

Constructor Modifiers Description

(constructor)(key, isLocked)

Constructs a new instance of the Binding class

Properties

Property Modifiers Type Description

isLocked

boolean

key

readonly

string

Key of the binding

providerConstructor

readonly

Constructor<Provider<T>> | undefined

For bindings bound via toProvider(), this property contains the constructor function of the provider class

scope

readonly

BindingScope

Scope of the binding to control how the value is cached/shared

source

readonly

BindingSource<T> | undefined

tagMap

readonly

TagMap

Map for tag name/value pairs

tagNames

readonly

string[]

Get an array of tag names

type

readonly

BindingType | undefined

Type of the binding value getter

valueConstructor

readonly

Constructor<T> | undefined

For bindings bound via toClass(), this property contains the constructor function of the class

Methods

Method Modifiers Description

apply(templateFns)

Apply one or more template functions to set up the binding with scope, tags, and other attributes as a group.

applyDefaultScope(scope)

Apply default scope to the binding. It only changes the scope if it’s not set yet

bind(key)

static

A static method to create a binding so that we can do Binding.bind('foo').to('bar'); as new Binding('foo').to('bar') is not easy to read.

configure(key)

static

Create a configuration binding for the given key

getValue(ctx, session)

This is an internal function optimized for performance. Users should use @inject(key) or ctx.get(key) instead.

Get the value bound to this key. Depending on isSync, this function returns either: - the bound value - a promise of the bound value

Consumers wishing to consume sync values directly should use isPromiseLike to check the type of the returned value to decide how to handle it.

getValue(ctx, options)

Returns a value or promise for this binding in the given context. The resolved value can be undefined if optional is set to true in options.

inScope(scope)

Set the binding scope

inspect(options)

Inspect the binding to return a json representation of the binding information

lock()

Lock the binding so that it cannot be rebound

on(eventName, listener)

The “changed” event is emitted by methods such as tag, inScope, to, and toClass.

on(event, listener)

once(eventName, listener)

The “changed” event is emitted by methods such as tag, inScope, to, and toClass.

once(event, listener)

refresh(ctx)

Invalidate the binding cache so that its value will be reloaded next time. This is useful to force reloading a cached value when its configuration or dependencies are changed. **WARNING**: The state held in the cached value will be gone.

tag(tags)

Tag the binding with names or name/value objects. A tag has a name and an optional value. If not supplied, the tag name is used as the value.

to(value)

Bind the key to a constant value. The value must be already available at binding time, it is not allowed to pass a Promise instance.

toAlias(keyWithPath)

Bind the key to an alias of another binding

toClass(ctor)

Bind the key to an instance of the given class.

toDynamicValue(factory)

Bind the key to a computed (dynamic) value.

toInjectable(ctor)

Bind to a class optionally decorated with @injectable. Based on the introspection of the class, it calls toClass/toProvider/toDynamicValue internally. The current binding key will be preserved (not being overridden by the key inferred from the class or options).

This is similar to createBindingFromClass() but applies to an existing binding.

toJSON()

Convert to a plain JSON object

toProvider(providerClass)

Bind the key to a value computed by a Provider.

*

unlock()

Unlock the binding