Page Contents

Home > @loopback/context > Context

Context class

Context provides an implementation of Inversion of Control (IoC) container

Signature:

export declare class Context extends EventEmitter 

Extends: EventEmitter

Constructors

Constructor Modifiers Description
[(constructor)(\_parent, name)](/doc/en/lb4/apidocs.context.context._constructor_.html) Create a new context.

Properties

Property Modifiers Type Description
[\_debug](/doc/en/lb4/apidocs.context.context._debug.html) `protected` Debugger A debug function which can be overridden by subclasses.
[\_parent?](/doc/en/lb4/apidocs.context.context._parent.html) `protected` [Context](/doc/en/lb4/apidocs.context.context.html) _(Optional)_ Parent context
[configResolver](/doc/en/lb4/apidocs.context.context.configresolver.html) `protected` [ConfigurationResolver](/doc/en/lb4/apidocs.context.configurationresolver.html) Configuration resolver
[name](/doc/en/lb4/apidocs.context.context.name.html) `readonly` string Name of the context
[registry](/doc/en/lb4/apidocs.context.context.registry.html) `protected` `readonly` Map<string, [Binding](/doc/en/lb4/apidocs.context.binding.html)> Key to binding map as the internal registry
[scope](/doc/en/lb4/apidocs.context.context.scope.html) [BindingScope](/doc/en/lb4/apidocs.context.bindingscope.html) Scope for binding resolution
[subscriptionManager](/doc/en/lb4/apidocs.context.context.subscriptionmanager.html) `readonly` [ContextSubscriptionManager](/doc/en/lb4/apidocs.context.contextsubscriptionmanager.html) Manager for observer subscriptions
[tagIndexer](/doc/en/lb4/apidocs.context.context.tagindexer.html) `protected` `readonly` ContextTagIndexer Indexer for bindings by tag

Methods

Method Modifiers Description
[\_findByTagIndex(tag)](/doc/en/lb4/apidocs.context.context._findbytagindex.html) `protected` Find bindings by tag leveraging indexes
[\_mergeWithParent(childList, parentList)](/doc/en/lb4/apidocs.context.context._mergewithparent.html) `protected`
[add(binding)](/doc/en/lb4/apidocs.context.context.add.html) Add a binding to the context. If a locked binding already exists with the same key, an error will be thrown.
[bind(key)](/doc/en/lb4/apidocs.context.context.bind.html) Create a binding with the given key in the context. If a locked binding already exists with the same key, an error will be thrown.
[close()](/doc/en/lb4/apidocs.context.context.close.html) Close the context: clear observers, stop notifications, and remove event listeners from its parent context.
[configure(key)](/doc/en/lb4/apidocs.context.context.configure.html) Create a corresponding binding for configuration of the target bound by the given key in the context. For example, `ctx.configure('controllers.MyController').to({x: 1})` will create binding `controllers.MyController:$config` with value `{x: 1}`.
[contains(key)](/doc/en/lb4/apidocs.context.context.contains.html) Check if a binding exists with the given key in the local context without delegating to the parent context
[createView(filter, comparator, options)](/doc/en/lb4/apidocs.context.context.createview.html) Create a view of the context chain with the given binding filter
[debug(args)](/doc/en/lb4/apidocs.context.context.debug.html) `protected` Wrap the debug statement so that it always print out the context name as the prefix
[emitError(err)](/doc/en/lb4/apidocs.context.context.emiterror.html) Emit an `error` event
[emitEvent(type, event)](/doc/en/lb4/apidocs.context.context.emitevent.html) A strongly-typed method to emit context events
[find(pattern)](/doc/en/lb4/apidocs.context.context.find.html) Find bindings using a key pattern or filter function
[findByTag(tagFilter)](/doc/en/lb4/apidocs.context.context.findbytag.html) Find bindings using the tag filter. If the filter matches one of the binding tags, the binding is included.
[findOrCreateBinding(key, policy)](/doc/en/lb4/apidocs.context.context.findorcreatebinding.html) Find or create a binding for the given key
[get(keyWithPath, session)](/doc/en/lb4/apidocs.context.context.get.html) Get the value bound to the given key, throw an error when no value is bound for the given key.
[get(keyWithPath, options)](/doc/en/lb4/apidocs.context.context.get_1.html) Get the value bound to the given key, optionally return a (deep) property of the bound value.
[getBinding(key)](/doc/en/lb4/apidocs.context.context.getbinding.html) Look up a binding by key in the context and its ancestors. If no matching binding is found, an error will be thrown.
[getBinding(key, options)](/doc/en/lb4/apidocs.context.context.getbinding_1.html) Look up a binding by key in the context and its ancestors. If no matching binding is found and `options.optional` is not set to true, an error will be thrown.
[getConfig(key, propertyPath, resolutionOptions)](/doc/en/lb4/apidocs.context.context.getconfig.html) Resolve configuration for the binding by key
[getConfigAsValueOrPromise(key, propertyPath, resolutionOptions)](/doc/en/lb4/apidocs.context.context.getconfigasvalueorpromise.html) Get the value or promise of configuration for a given binding by key
[getConfigSync(key, propertyPath, resolutionOptions)](/doc/en/lb4/apidocs.context.context.getconfigsync.html) Resolve configuration synchronously for the binding by key
[getDebugNamespace()](/doc/en/lb4/apidocs.context.context.getdebugnamespace.html) `protected` Get the debug namespace for the context class. Subclasses can override this method to supply its own namespace.
[getOwnerContext(keyOrBinding)](/doc/en/lb4/apidocs.context.context.getownercontext.html) Get the owning context for a binding or its key
[getResolutionContext(binding)](/doc/en/lb4/apidocs.context.context.getresolutioncontext.html) Locate the resolution context for the given binding. Only bindings in the resolution context and its ancestors are visible as dependencies to resolve the given binding
[getScopedContext(scope)](/doc/en/lb4/apidocs.context.context.getscopedcontext.html) Get the context matching the scope
[getSync(keyWithPath, session)](/doc/en/lb4/apidocs.context.context.getsync.html) Get the synchronous value bound to the given key, optionally return a (deep) property of the bound value. This method throws an error if the bound value requires async computation (returns a promise). You should never rely on sync bindings in production code.
[getSync(keyWithPath, options)](/doc/en/lb4/apidocs.context.context.getsync_1.html) Get the synchronous value bound to the given key, optionally return a (deep) property of the bound value. This method throws an error if the bound value requires async computation (returns a promise). You should never rely on sync bindings in production code.
[inspect(options)](/doc/en/lb4/apidocs.context.context.inspect.html) Inspect the context and dump out a JSON object representing the context hierarchy
[isBound(key)](/doc/en/lb4/apidocs.context.context.isbound.html) Check if a key is bound in the context or its ancestors
[isSubscribed(observer)](/doc/en/lb4/apidocs.context.context.issubscribed.html) Check if an observer is subscribed to this context
[isVisibleTo(ctx)](/doc/en/lb4/apidocs.context.context.isvisibleto.html) Check if this context is visible (same or ancestor) to the given one
[on(eventName, listener)](/doc/en/lb4/apidocs.context.context.on.html) The "bind" event is emitted when a new binding is added to the context. The "unbind" event is emitted when an existing binding is removed.
[on(event, listener)](/doc/en/lb4/apidocs.context.context.on_1.html)
[once(eventName, listener)](/doc/en/lb4/apidocs.context.context.once.html) The "bind" event is emitted when a new binding is added to the context. The "unbind" event is emitted when an existing binding is removed.
[once(event, listener)](/doc/en/lb4/apidocs.context.context.once_1.html)
[setupConfigurationResolverIfNeeded()](/doc/en/lb4/apidocs.context.context.setupconfigurationresolverifneeded.html) `protected` Set up the configuration resolver if needed
[subscribe(observer)](/doc/en/lb4/apidocs.context.context.subscribe.html) Add a context event observer to the context
[toJSON()](/doc/en/lb4/apidocs.context.context.tojson.html) Create a plain JSON object for the context
[unbind(key)](/doc/en/lb4/apidocs.context.context.unbind.html) Unbind a binding from the context. No parent contexts will be checked.
[unsubscribe(observer)](/doc/en/lb4/apidocs.context.context.unsubscribe.html) Remove the context event observer from the context