templates
Advanced tools
Comparing version 0.1.26 to 0.2.0
109
index.js
@@ -63,2 +63,16 @@ /*! | ||
/** | ||
* Decorate methods onto the Templates prototype | ||
*/ | ||
decorate.option(Templates.prototype); | ||
decorate.routes(Templates.prototype); | ||
decorate.engine(Templates.prototype); | ||
decorate.context(Templates.prototype); | ||
decorate.helpers(Templates.prototype); | ||
decorate.layout(Templates.prototype); | ||
decorate.render(Templates.prototype); | ||
decorate.lookup(Templates.prototype); | ||
decorate.errors(Templates.prototype, 'Templates'); | ||
/** | ||
* Initialize Templates default configuration | ||
@@ -68,11 +82,11 @@ */ | ||
Templates.prototype.defaultConfig = function () { | ||
// used in plugins to verify the app instance | ||
this.define('isApp', true); | ||
decorate.init(this); | ||
this.inflections = {}; | ||
decorate.init(this); | ||
this.items = {}; | ||
this.views = {}; | ||
for (var key in this.options.mixins) { | ||
this.mixin(key, this.options.mixins[key]); | ||
} | ||
this.initialize(); | ||
this.initialize(this.options); | ||
this.listen(this); | ||
@@ -82,16 +96,2 @@ }; | ||
/** | ||
* Decorate methods onto the Templates prototype | ||
*/ | ||
decorate.config(Templates.prototype); | ||
decorate.routes(Templates.prototype); | ||
decorate.engine(Templates.prototype); | ||
decorate.context(Templates.prototype); | ||
decorate.helpers(Templates.prototype); | ||
decorate.layout(Templates.prototype); | ||
decorate.render(Templates.prototype); | ||
decorate.lookup(Templates.prototype); | ||
decorate.errors(Templates.prototype, 'Templates'); | ||
/** | ||
* Initialize defaults. Exposes constructors on | ||
@@ -104,7 +104,7 @@ * app instance. | ||
this.define('Item', this.options.Item || Item); | ||
this.define('List', this.options.List || List); | ||
this.define('View', this.options.View || View); | ||
this.define('List', this.options.List || List); | ||
this.define('Collection', this.options.Collection || Collection); | ||
this.define('Group', this.options.Group || Group); | ||
this.define('Views', this.options.Views || Views); | ||
this.define('Group', this.options.Group || Group); | ||
}; | ||
@@ -121,3 +121,3 @@ | ||
} | ||
utils.optionUpdated(app, key, value); | ||
utils.updateOptions(app, key, value); | ||
}); | ||
@@ -165,5 +165,5 @@ | ||
* ```js | ||
* var view = app.view('foo', {conetent: '...'}); | ||
* var view = app.view('foo', {content: '...'}); | ||
* // or | ||
* var view = app.view({path: 'foo', conetent: '...'}); | ||
* var view = app.view({path: 'foo', content: '...'}); | ||
* ``` | ||
@@ -212,20 +212,22 @@ * @name .view | ||
Templates.prototype.collection = function (opts) { | ||
Templates.prototype.collection = function (opts, created) { | ||
opts = opts || {}; | ||
if (!opts.views && !opts.options) { | ||
if (!opts.isCollection) { | ||
utils.defaults(opts, this.options); | ||
} | ||
var Collection = opts.Collection || this.get('Collection'); | ||
var Collection = opts.Collection || this.get('Views'); | ||
var collection = {}; | ||
if (opts instanceof Collection) { | ||
if (opts.isCollection === true) { | ||
collection = opts; | ||
} else { | ||
opts.Item = opts.Item || this.get('Item'); | ||
opts.Item = opts.Item || this.get('View'); | ||
collection = new Collection(opts); | ||
} | ||
this.extendViews(collection, opts); | ||
if (created !== true) { | ||
this.extendViews(collection, opts); | ||
} | ||
@@ -238,43 +240,2 @@ // emit the collection | ||
/** | ||
* Create a new view collection. View collections are decorated | ||
* with special methods for getting, setting and rendering | ||
* views from that collection. Collections created with this method | ||
* are not stored on `app.views` as with the [create](#create) method. | ||
* | ||
* See the [collection docs](docs/collections.md#view-collections) for more | ||
* information about view collections. | ||
* | ||
* @name .viewCollection | ||
* @param {Object} `opts` View collection options. | ||
* @return {Object} Returns the view collection instance for chaining. | ||
* @api public | ||
*/ | ||
Templates.prototype.viewCollection = function (opts, created) { | ||
opts = opts || {}; | ||
if (!opts.views && !opts.options) { | ||
utils.defaults(opts, this.options); | ||
} | ||
var Views = opts.Views || this.get('Views'); | ||
var views = {}; | ||
if (opts instanceof Views) { | ||
views = opts; | ||
} else { | ||
opts.View = opts.View || this.get('View'); | ||
views = new Views(opts); | ||
} | ||
if (created !== true) { | ||
this.extendViews(views, opts); | ||
} | ||
// emit the views | ||
this.emit('viewCollection', views, opts); | ||
return views; | ||
}; | ||
/** | ||
* Create a new view collection to be stored on the `app.views` object. See | ||
@@ -293,7 +254,7 @@ * the [create docs](docs/collections.md#create) for more details. | ||
opts = opts || {}; | ||
if (!opts.views && !opts.options) { | ||
if (!opts.views && !opts.items && !opts.options) { | ||
utils.defaults(opts, this.options); | ||
} | ||
var collection = this.viewCollection(opts, true); | ||
var collection = this.collection(opts, true); | ||
var self = this; | ||
@@ -316,3 +277,3 @@ | ||
// add the collection to `app.views` | ||
this.views[plural] = collection.views; | ||
this.views[plural] = collection.items || collection.views; | ||
@@ -367,3 +328,3 @@ // create loader functions for adding views to this collection | ||
/** | ||
* Mix in a prototype method | ||
* Add a property to the `Templates` prototype | ||
*/ | ||
@@ -370,0 +331,0 @@ |
@@ -21,11 +21,25 @@ 'use strict'; | ||
function Collection(options) { | ||
if (!(this instanceof Collection)) { | ||
return new Collection(options); | ||
} | ||
Base.call(this); | ||
options = options || {}; | ||
// decorate the instance | ||
utils.renameKey(this); | ||
this.init(options); | ||
} | ||
// add constructors | ||
this.define('List', options.List || require('./list')); | ||
this.define('Item', options.Item || require('./item')); | ||
/** | ||
* Inherit `Base` | ||
*/ | ||
Base.extend(Collection); | ||
/** | ||
* Initialize `Collection` defaults | ||
*/ | ||
Collection.prototype.init = function(opts) { | ||
this.define('List', opts.List || require('./list')); | ||
this.define('Item', opts.Item || require('./item')); | ||
this.define('loaded', false); | ||
@@ -38,19 +52,16 @@ this.define('plugins', []); | ||
// if an instance of `List` of `Collection` is passed, load it now | ||
if (Array.isArray(options) || options instanceof this.List) { | ||
this.options = options.options; | ||
this.addList(options.items); | ||
// if an instance of `List` or `Collection` is passed, load it now | ||
if (Array.isArray(opts) || opts instanceof this.List) { | ||
this.options = opts.options; | ||
this.addList(opts.items); | ||
} else if (options instanceof Collection) { | ||
this.options = options.options; | ||
this.addItems(options.items); | ||
} else if (opts instanceof Collection) { | ||
this.options = opts.options; | ||
this.addItems(opts.items); | ||
} else { | ||
this.options = options; | ||
this.options = opts; | ||
} | ||
} | ||
}; | ||
Base.extend(Collection); | ||
decorate.config(Collection.prototype); | ||
/** | ||
@@ -89,9 +100,16 @@ * Run a plugin on the collection instance. Plugins | ||
/** | ||
* Returns a new item, using the `Item` class | ||
* currently defined on the instance. | ||
* Add ann `option` method to the prototype | ||
*/ | ||
decorate.option(Collection.prototype); | ||
/** | ||
* Returns a new item, using the `Item` class currently defined | ||
* on the instance. Items created using this method are not cached | ||
* on the instance. To cache an item, use [setItem](#setItem) | ||
* | ||
* ```js | ||
* var item = app.item('foo', {conetent: '...'}); | ||
* var item = app.item('foo', {content: '...'}); | ||
* // or | ||
* var item = app.item({path: 'foo', conetent: '...'}); | ||
* var item = app.item({path: 'foo', content: '...'}); | ||
* ``` | ||
@@ -109,3 +127,4 @@ * @name .item | ||
* Set an item on the collection. This is identical to [addItem](#addItem) | ||
* except `setItem` does not emit an event for each item. | ||
* except `setItem` does not emit an event for each item and does not | ||
* iterate over the item `queue`. | ||
* | ||
@@ -118,3 +137,3 @@ * ```js | ||
* @param {Object} `value` If key is a string, value is the item object. | ||
* @developer This method is decorated onto the collection in the constructor using the `createItem` utility method. | ||
* @developer The `item` method is decorated onto the collection using the `itemFactory` utility method. | ||
* @return {Object} returns the `item` instance. | ||
@@ -218,3 +237,3 @@ * @api public | ||
Collection.prototype.getItem = function(key) { | ||
return this.items[key] || this.items[this.renameKey.call(this, key)]; | ||
return this.items[key] || this.items[this.renameKey(key)]; | ||
}; |
@@ -9,4 +9,2 @@ 'use strict'; | ||
app.cache.context = {}; | ||
app.items = {}; | ||
app.views = {}; | ||
@@ -13,0 +11,0 @@ init(app); |
'use strict'; | ||
var config = require('./config'); | ||
var option = require('./option'); | ||
var utils = require('../utils'); | ||
@@ -14,3 +14,3 @@ | ||
if (typeof view.option !== 'function') { | ||
config(view); | ||
option(view); | ||
utils.defaults(view.options, options); | ||
@@ -17,0 +17,0 @@ } |
@@ -11,5 +11,3 @@ 'use strict'; | ||
* var group = new Group({ | ||
* 'foo': { | ||
* items: [1,2,3] | ||
* } | ||
* 'foo': { items: [1,2,3] } | ||
* }); | ||
@@ -16,0 +14,0 @@ * ``` |
@@ -33,3 +33,3 @@ 'use strict'; | ||
this.define('_name', 'Item'); | ||
this.define('_name', item._name || 'Item'); | ||
this.define('_contents', null); | ||
@@ -67,3 +67,3 @@ this.define('_content', null); | ||
Base.inherit(Item, Vinyl); | ||
decorate.config(Item.prototype); | ||
decorate.option(Item.prototype); | ||
@@ -74,3 +74,3 @@ /** | ||
* ```js | ||
* var item = new Item({path: 'abc', contents: '...'}) | ||
* var item = new Item({path: 'abc', content: '...'}) | ||
* .use(require('foo')) | ||
@@ -80,4 +80,4 @@ * .use(require('bar')) | ||
* ``` | ||
* @param {Function} `fn` | ||
* @return {Object} | ||
* @param {Function} `fn` plugin function to call | ||
* @return {Object} Returns the item instance for chaining. | ||
* @api public | ||
@@ -84,0 +84,0 @@ */ |
@@ -9,8 +9,16 @@ 'use strict'; | ||
/** | ||
* Expose `List` | ||
* Create an instance of `List` with the given options. | ||
* Lists differ from collections in that items are stored | ||
* as an array, allowing items to be paginated, sorted, | ||
* and grouped. | ||
* | ||
* ```js | ||
* var list = new List(); | ||
* list.addItem('foo', {content: 'bar'}); | ||
* ``` | ||
* @param {Object} `options` | ||
* @api public | ||
*/ | ||
module.exports = List; | ||
function List(options) { | ||
var List = module.exports = function List(options) { | ||
Base.call(this); | ||
@@ -53,3 +61,3 @@ options = options || {}; | ||
decorate.config(List.prototype); | ||
decorate.option(List.prototype); | ||
decorate.routes(List.prototype); | ||
@@ -101,5 +109,5 @@ decorate.engine(List.prototype); | ||
* ```js | ||
* var item = app.item('foo', {conetent: '...'}); | ||
* var item = app.item('foo', {content: '...'}); | ||
* // or | ||
* var item = app.item({path: 'foo', conetent: '...'}); | ||
* var item = app.item({path: 'foo', content: '...'}); | ||
* ``` | ||
@@ -132,3 +140,4 @@ * @name .item | ||
var item = this.item(key, value); | ||
item = addPaging(item, this.items); | ||
addPager(item, this.items); | ||
this.keys.push(item.key); | ||
@@ -140,10 +149,2 @@ this.items.push(item); | ||
/** | ||
* Adds event emitting and custom loading to [setItem](#setItem). | ||
* | ||
* @param {String} `key` | ||
* @param {Object} `value` | ||
* @api public | ||
*/ | ||
/** | ||
* Add an item to the list. An item may be an instance of | ||
@@ -365,3 +366,3 @@ * `Item`, and if not the item is converted to an instance of | ||
function addPaging(item, items) { | ||
function addPager(item, items) { | ||
item.data.pager = {}; | ||
@@ -376,3 +377,2 @@ item.data.pager.index = items.length; | ||
} | ||
return item; | ||
} |
'use strict'; | ||
var keys = require('./keys'); | ||
/** | ||
@@ -117,11 +119,11 @@ * Expose common utils | ||
/** | ||
* Handle when an option has been updated by updating any underlying properties | ||
* that may rely on those options | ||
* When a constructor is defined after init, update any underlying | ||
* properties that may rely on that option (constructor). | ||
*/ | ||
utils.optionUpdated = function optionUpdated(app, key, value) { | ||
var keys = require('./keys').constructorKeys; | ||
if (keys.indexOf(key) > -1) { | ||
utils.updateOptions = function(app, key, value) { | ||
var k = keys.constructorKeys; | ||
if (k.indexOf(key) > -1) { | ||
app.define(key, value); | ||
} | ||
}; |
@@ -18,5 +18,7 @@ 'use strict'; | ||
utils.resolveGlob = function resolveGlob(patterns, options) { | ||
var opts = lazy.extend({cwd: process.cwd()}, options); | ||
return lazy.globby.sync(patterns, opts).map(function (fp) { | ||
utils.resolveGlob = function(patterns, options) { | ||
var opts = lazy.extend({cwd: ''}, options); | ||
var files = lazy.globby.sync(patterns, opts); | ||
return files.map(function (fp) { | ||
return path.resolve(opts.cwd, fp); | ||
@@ -27,17 +29,6 @@ }); | ||
/** | ||
* Try to read a file, fail gracefully | ||
*/ | ||
utils.tryRead = function(fp) { | ||
try { | ||
return fs.readFileSync(fp); | ||
} catch(err) {} | ||
return null; | ||
}; | ||
/** | ||
* Require a glob of files | ||
*/ | ||
utils.requireGlob = function requireGlob(patterns, options) { | ||
utils.requireGlob = function(patterns, options) { | ||
return utils.resolveGlob(patterns, options) | ||
@@ -57,3 +48,3 @@ .reduce(function (acc, fp) { | ||
utils.requireData = function requireData(patterns, opts) { | ||
utils.requireData = function(patterns, opts) { | ||
opts.rename = opts.namespace || opts.renameKey || function (key) { | ||
@@ -69,3 +60,3 @@ return path.basename(key, path.extname(key)); | ||
utils.tryRequire = function tryRequire(fp) { | ||
utils.tryRequire = function(fp) { | ||
try { | ||
@@ -81,1 +72,12 @@ return require(fp); | ||
}; | ||
/** | ||
* Try to read a file, fail gracefully | ||
*/ | ||
utils.tryRead = function(fp) { | ||
try { | ||
return fs.readFileSync(fp); | ||
} catch(err) {} | ||
return null; | ||
}; |
@@ -39,3 +39,3 @@ 'use strict'; | ||
/** | ||
* Return true if the given value is a view. | ||
* Add a `renameKey` function to the given `app` instance. | ||
*/ | ||
@@ -50,10 +50,6 @@ | ||
if (this.option && typeof fn !== 'function') { | ||
fn = this.option('renameKey'); | ||
} | ||
if (typeof fn !== 'function') { | ||
fn = common.identity; | ||
fn = this.option('renameKey') || common.identity; | ||
} | ||
this.options = this.options || {}; | ||
this.options.renameKey = fn; | ||
@@ -60,0 +56,0 @@ if (typeof key === 'string') { |
@@ -53,3 +53,3 @@ 'use strict'; | ||
/** | ||
* Decorate a `view` method onto the given `app` object. | ||
* Decorate an "item" method onto the given `collection` object. | ||
* | ||
@@ -56,0 +56,0 @@ * @param {Object} app |
'use strict'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
@@ -54,3 +53,3 @@ var Base = require('base-methods'); | ||
decorate.config(Views.prototype); | ||
decorate.option(Views.prototype); | ||
decorate.routes(Views.prototype); | ||
@@ -57,0 +56,0 @@ decorate.engine(Views.prototype); |
{ | ||
"name": "templates", | ||
"description": "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.", | ||
"version": "0.1.26", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/templates", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
123411
36
3149