templates
Version:
System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.
105 lines (89 loc) • 2.24 kB
JavaScript
;
var Base = require('base-methods');
var utils = require('./utils');
/**
* Create an instance of `Group` with the given `options`.
*
* ```js
* var group = new Group({
* 'foo': {
* items: [1,2,3]
* }
* });
* ```
* @param {Object} `options`
* @api public
*/
function Group(options) {
Base.call(this, options);
this.define('List', this.List || require('./list'));
this.define('plugins', []);
}
/**
* Inherit `Base`
*/
Base.extend(Group);
/**
* Run a plugin on the group instance. Plugins
* are invoked immediately upon creating the group
* in the order in which they were defined.
*
* ```js
* group.use(function(group) {
* // `group` is the instance, as is `this`
* });
* ```
*
* @param {Function} `fn` Plugin function. If the plugin returns a function it will be passed to the `use` method of each view created on the instance.
* @return {Object} Returns the instance for chaining.
* @api public
*/
Group.prototype.use = function(fn) {
var plugin = fn.call(this, this, this.options);
if (typeof plugin === 'function') {
this.plugins.push(plugin);
}
this.emit('use');
return this;
};
/**
* Get a value from the group instance. If the value is an array,
* it will be returned as a new `List`.
*
* @return {[type]}
*/
Group.prototype.get = function(key) {
var res = Base.prototype.get.apply(this, arguments);
if (Array.isArray(res)) {
var List = this.List;
var list = new List();
list.addItems(res);
return list;
}
handleErrors(this, res);
return res;
};
/**
* When `get` returns a non-Array object, we decorate
* noop `List` methods onto the object to inform the
* user that list methods do not work on groups.
*
* @param {Object} `group`
* @param {Object} `val` Value returned from `group.get()`
*/
function handleErrors(group, val) {
if (typeof val === 'object') {
var List = group.List;
var keys = Object.keys(List.prototype);
keys.forEach(function(key) {
if (typeof val[key] !== 'undefined') return;
utils.define(val, key, function() {
throw new Error(key + ' can only be used with an array of `List` items.');
});
});
}
}
/**
* Expose `Group`
*/
module.exports = Group;