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> 

Constructors

Constructor Modifiers Description
(constructor)(key, isLocked)   Constructs a new instance of the Binding class

Properties

Property Modifiers Type Description
isLocked   boolean  
key   string Key of the binding
scope   BindingScope Scope of the binding to control how the value is cached/shared
tagMap   TagMap Map for tag name/value pairs
tagNames   string[] Get an array of tag names
type   BindingType | undefined Type of the binding value getter
valueConstructor   Constructor<T> | undefined For bindings bound via toClass, this property contains the constructor function

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 valueConsumers 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
lock()   Lock the binding so that it cannot be rebound
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(factoryFn)   Bind the key to a computed (dynamic) value.
toJSON()   Convert to a plain JSON object
toProvider(providerClass)   Bind the key to a value computed by a Provider.*
unlock()   Unlock the binding