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)](/doc/en/lb4/apidocs.context.binding._constructor_.html) Constructs a new instance of the `Binding` class

Properties

Property Modifiers Type Description
[isLocked](/doc/en/lb4/apidocs.context.binding.islocked.html) boolean
[key](/doc/en/lb4/apidocs.context.binding.key.html) `readonly` string Key of the binding
[providerConstructor](/doc/en/lb4/apidocs.context.binding.providerconstructor.html) `readonly` [Constructor](/doc/en/lb4/apidocs.context.constructor.html)<[Provider](/doc/en/lb4/apidocs.context.provider.html)<T>> \| undefined For bindings bound via `toProvider()`, this property contains the constructor function of the provider class
[scope](/doc/en/lb4/apidocs.context.binding.scope.html) `readonly` [BindingScope](/doc/en/lb4/apidocs.context.bindingscope.html) Scope of the binding to control how the value is cached/shared
[source](/doc/en/lb4/apidocs.context.binding.source.html) `readonly` [BindingSource](/doc/en/lb4/apidocs.context.bindingsource.html)<T> \| undefined
[tagMap](/doc/en/lb4/apidocs.context.binding.tagmap.html) `readonly` [TagMap](/doc/en/lb4/apidocs.context.tagmap.html) Map for tag name/value pairs
[tagNames](/doc/en/lb4/apidocs.context.binding.tagnames.html) `readonly` string\[\] Get an array of tag names
[type](/doc/en/lb4/apidocs.context.binding.type.html) `readonly` [BindingType](/doc/en/lb4/apidocs.context.bindingtype.html) \| undefined Type of the binding value getter
[valueConstructor](/doc/en/lb4/apidocs.context.binding.valueconstructor.html) `readonly` [Constructor](/doc/en/lb4/apidocs.context.constructor.html)<T> \| undefined For bindings bound via `toClass()`, this property contains the constructor function of the class

Methods

Method Modifiers Description
[apply(templateFns)](/doc/en/lb4/apidocs.context.binding.apply.html) Apply one or more template functions to set up the binding with scope, tags, and other attributes as a group.
[applyDefaultScope(scope)](/doc/en/lb4/apidocs.context.binding.applydefaultscope.html) Apply default scope to the binding. It only changes the scope if it's not set yet
[bind(key)](/doc/en/lb4/apidocs.context.binding.bind.html) `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)](/doc/en/lb4/apidocs.context.binding.configure.html) `static` Create a configuration binding for the given key
[getValue(ctx, session)](/doc/en/lb4/apidocs.context.binding.getvalue.html) 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)](/doc/en/lb4/apidocs.context.binding.getvalue_1.html) 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)](/doc/en/lb4/apidocs.context.binding.inscope.html) Set the binding scope
[inspect(options)](/doc/en/lb4/apidocs.context.binding.inspect.html) Inspect the binding to return a json representation of the binding information
[lock()](/doc/en/lb4/apidocs.context.binding.lock.html) Lock the binding so that it cannot be rebound
[on(eventName, listener)](/doc/en/lb4/apidocs.context.binding.on.html) The "changed" event is emitted by methods such as `tag`, `inScope`, `to`, and `toClass`.
[on(event, listener)](/doc/en/lb4/apidocs.context.binding.on_1.html)
[once(eventName, listener)](/doc/en/lb4/apidocs.context.binding.once.html) The "changed" event is emitted by methods such as `tag`, `inScope`, `to`, and `toClass`.
[once(event, listener)](/doc/en/lb4/apidocs.context.binding.once_1.html)
[refresh(ctx)](/doc/en/lb4/apidocs.context.binding.refresh.html) 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)](/doc/en/lb4/apidocs.context.binding.tag.html) 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)](/doc/en/lb4/apidocs.context.binding.to.html) 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)](/doc/en/lb4/apidocs.context.binding.toalias.html) Bind the key to an alias of another binding
[toClass(ctor)](/doc/en/lb4/apidocs.context.binding.toclass.html) Bind the key to an instance of the given class.
[toDynamicValue(factory)](/doc/en/lb4/apidocs.context.binding.todynamicvalue.html) Bind the key to a computed (dynamic) value.
[toInjectable(ctor)](/doc/en/lb4/apidocs.context.binding.toinjectable.html) 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()](/doc/en/lb4/apidocs.context.createbindingfromclass.html) but applies to an existing binding.
[toJSON()](/doc/en/lb4/apidocs.context.binding.tojson.html) Convert to a plain JSON object
[toProvider(providerClass)](/doc/en/lb4/apidocs.context.binding.toprovider.html) Bind the key to a value computed by a Provider. \*
[unlock()](/doc/en/lb4/apidocs.context.binding.unlock.html) Unlock the binding