Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connect to #260
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,59 @@ | ||
# @loopback/context | ||
|
||
Loopback uses Context to manage state and dependencies in your application. | ||
|
||
## Overview | ||
|
||
- An abstraction of all state and dependencies in your application. | ||
- Context is what LB uses to "manage" everything. | ||
- A global registry for anything/everything in your app (all configs, state, dependencies, classes, etc). | ||
- An IoC container used to inject dependencies into your code. | ||
|
||
## Installation | ||
|
||
``` | ||
$ npm install --save @loopback/context | ||
``` | ||
|
||
## Basic use | ||
``` | ||
// app level | ||
const app = new Application(); // `app` is a "Context" | ||
app.bind('hello').to('world'); // ContextKey='hello', ContextValue='world' | ||
console.log(app.getSync('hello')); // => 'world' | ||
``` | ||
|
||
Dependency injection using context | ||
``` | ||
const Application = require('@loopback/core'); | ||
const app = new Application(); | ||
const app.bind('defaultName').to('John'); // setting context | ||
class HelloController { | ||
// consider this.ctx here | ||
constructor(@inject('defaultName') name) { // injecting dependency using context | ||
this.name = name; | ||
} | ||
greet(name) { | ||
return `Hello ${name || this.name}`; | ||
} | ||
} | ||
``` | ||
|
||
For additional information, please refer to the [wiki](https://github.com/strongloop/loopback-next/wiki/Context). | ||
|
||
## Contributions | ||
|
||
IBM/StrongLoop is an active supporter of open source and welcomes contributions to our projects as well as those of the Node.js community in general. For more information on how to contribute please refer to the [Contribution Guide](https://loopback.io/doc/en/contrib/index.html). | ||
|
||
## Tests | ||
|
||
Run `npm test` from the root folder. | ||
|
||
## Contributors | ||
|
||
See [all contributors](https://github.com/strongloop/loopback-next/graphs/contributors). | ||
|
||
## License | ||
|
||
MIT |