Skip to content

Latest commit

 

History

History
100 lines (70 loc) · 2.05 KB

.verb.md

File metadata and controls

100 lines (70 loc) · 2.05 KB

Usage

Adds a data method to [base][] that can be used for setting, getting and loading data onto a specified object in your application.

var Base = require('base');
var data = require('{%= name %}');

// instantiate `Base`
var base = new Base();
// add `data` as a plugin
base.use(data());

Examples

Add data:

app.data('a', 'b');
app.data({c: 'd'});
app.data('e', ['f']);
console.log(app.cache.data);
//=> {a: 'b', c: 'd', e: ['f']}

cache.data

By default, all data is loaded onto app.cache.data. This can be customized by passing the property to use when the plugin is initialized.

For example, the following set app.foo as object for storing data:

app.use(data('foo'));
app.data('a', 'b');
console.log(app.foo);
//=> {a: 'b'}

API

{%= apidocs("index.js") %}

Glob patterns

Glob patterns may be passed as a string or array. All of these work:

app.data('foo.json');
app.data('*.json');
app.data(['*.json']);
// pass options to node-glob
app.data(['*.json'], {dot: true});

Namespacing

Namespacing allows you to load data onto a specific key, optionally using part of the file path as the key.

Example

Given that foo.json contains {a: 'b'}:

app.data('foo.json');
console.log(app.cache.data);
//=> {a: 'b'}

app.data('foo.json', {namespace: true});
console.log(app.cache.data);
//=> {foo: {a: 'b'}}

app.data('foo.json', {
  namespace: function(fp) {
    return path.basename(fp);
  }
});
console.log(app.cache.data);
//=> {'foo.json': {a: 'b'}}

History

v0.6.0

  • removes renameKey option for namespacing
  • replaces [is-valid-instance][] and [is-registered][] with [is-valid-app][]

v0.5.0

  • uses [is-valid-instance][] and [is-registered][] to ensure the smart plugin is registered on the correct objects.

v0.4.0

  • Refactored
  • adds methods to .data for getting and setting data.

v0.3.6

  • adds a basic loader that only calls the JSON.parse method, if no other loaders are defined
  • calls .isRegistered from [base][] to ensure the plugin is only loaded once on an instance