Package Exports
- base-data
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (base-data) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
base-data 
adds a
data
method to base-methods.
Adds a data
method to base-methods that can be used for setting, getting and loading data onto a specified object in your application.
Install
Install with npm
$ npm i base-data --save
Usage
var Base = require('base-methods');
var data = require('base-data');
// create your application and inherit `Base`
function App() {
Base.call(this);
}
Base.extend(App);
var app = new App();
// add the `data` method to `App`
// note that `data()` is a function that is called
app.mixin('data', data());
// set/get/load data
app.data('foo', 'bar');
app.data({a: 'b'});
console.log(app.cache.data);
//=> {foo: 'bar', a: 'b'}
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'}}
API
.data
Params
key
{String|Object}: Pass a key-value pair or an object to set.val
{any}: Any value when a key-value pair is passed. This can also be options if a glob pattern is passed as the first value.returns
{Object}: Returns an instance ofTemplates
for chaining.
Example
app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}
Related projects
- base-methods: Starter for creating a node.js application with a handful of common methods, like
set
,get
,… more | homepage - base-options: Adds an
option
method to base-methods. | homepage - class-utils: Utils for working with JavaScript classes and prototype methods. | homepage
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Author
Jon Schlinkert
License
Copyright © 2015 Jon Schlinkert Released under the MIT license.
This file was generated by verb-cli on October 06, 2015.