An open API service indexing awesome lists of open source software.

https://github.com/doowb/context-manager

Manage context for templates.
https://github.com/doowb/context-manager

Last synced: 11 months ago
JSON representation

Manage context for templates.

Awesome Lists containing this project

README

          

# context-manager [![NPM version](https://badge.fury.io/js/context-manager.svg)](http://badge.fury.io/js/context-manager)

> Manage context for templates.

## Install with [npm](npmjs.org)

```bash
npm i context-manager --save
```

## Usage

```js
var context = require('context-manager');
```

## API
### [Context](index.js#L20)

Create an instance of `Context`.

### [.setContext](index.js#L40)

Add a context level, optionally passing a value to start with.

* `name` **{String}**: The name of the context to add.
* `level` **{Number}**: Numerical value representing the order in which this level should be merged versus other lvl.
* `value` **{Object}**: Optionally pass an object to start with.

```js
context.setContext('locals', {a: 'b'});
```

### [.getContext](index.js#L60)

Get the raw (un-merged) context for `name`.

* `name` **{String}**: The context to get.
* `returns`: {*}

```js
context.setContext('a', {a: 'b'});
context.getContext('a');
// => {a: 'b'}
```

### [.extendContext](index.js#L78)

Extend context `name` with the given `value`

* `name` **{String}**
* `key` **{String}**
* `value` **{Object}**
* `returns`: {String}

```js
context.setContext('locals', {a: 'b'});
```

### [.setLevel](index.js#L107)

Set the level for a context. This determines the order in which the context will be merged when `.calculate()` is called.

* `name` **{String}**: The name of the context.
* `level` **{Number}**: The level (number) to set for the level.

```js
if (foo) {
context.setLevel('locals', 10);
} else {
context.setLevel('locals', 0);
}
```

### [.calculate](index.js#L142)

Calculate the context, optionally passing a callback `fn` for sorting. _(Note that sorting must be done on levels, not on the context names)_.

* `keys` **{String|Array}**: Key, or array of keys for context levels to include.
* `fn` **{Function}**: Sort function for determining the order of merging.

```js
app.calculate(['a', 'b'], function(a, b) {
return app.lvl[a] - app.lvl[a];
});
```

### [.resetContexts](index.js#L166)

Clear all contexts.

## Author

**Jon Schlinkert**

+ [github/assemble](https://github.com/assemble)
+ [twitter/assemble](http://twitter.com/assemble)

## License
Copyright (c) 2015 Jon Schlinkert
Released under the license

***

_This file was generated by [verb](https://github.com/assemble/verb) on February 21, 2015._