3 var util = require('util');
4 var define = require('define-property');
5 var CacheBase = require('cache-base');
6 var Emitter = require('component-emitter');
7 var isObject = require('isobject');
8 var merge = require('mixin-deep');
9 var pascal = require('pascalcase');
10 var cu = require('class-utils');
13 * Optionally define a custom `cache` namespace to use.
16 function namespace(name) {
17 var Cache = name ? CacheBase.namespace(name) : CacheBase;
21 * Create an instance of `Base` with the given `config` and `options`.
24 * // initialize with `config` and `options`
25 * var app = new Base({isApp: true}, {abc: true});
26 * app.set('foo', 'bar');
28 * // values defined with the given `config` object will be on the root of the instance
29 * console.log(app.baz); //=> undefined
30 * console.log(app.foo); //=> 'bar'
32 * console.log(app.get('isApp')); //=> true
33 * console.log(app.get('foo')); //=> 'bar'
35 * // values defined with the given `options` object will be on `app.options
36 * console.log(app.options.abc); //=> true
39 * @param {Object} `config` If supplied, this object is passed to [cache-base][] to merge onto the the instance upon instantiation.
40 * @param {Object} `options` If supplied, this object is used to initialize the `base.options` object.
44 function Base(config, options) {
45 if (!(this instanceof Base)) {
46 return new Base(config, options);
48 Cache.call(this, config);
50 this.initBase(config, options);
57 util.inherits(Base, Cache);
60 * Add static emitter methods
66 * Initialize `Base` defaults with the given `config` object
69 Base.prototype.initBase = function(config, options) {
70 this.options = merge({}, this.options, options);
71 this.cache = this.cache || {};
72 this.define('registered', {});
73 if (name) this[name] = {};
75 // make `app._callbacks` non-enumerable
76 this.define('_callbacks', this._callbacks);
77 if (isObject(config)) {
78 this.visit('set', config);
80 Base.run(this, 'use', fns);
84 * Set the given `name` on `app._name` and `app.is*` properties. Used for doing
89 * console.log(app._name);
91 * console.log(app.isFoo);
94 * console.log(app.isFoo);
96 * console.log(app.isBar);
98 * console.log(app._name);
102 * @param {String} `name`
107 Base.prototype.is = function(name) {
108 if (typeof name !== 'string') {
109 throw new TypeError('expected name to be a string');
111 this.define('is' + pascal(name), true);
112 this.define('_name', name);
113 this.define('_appname', name);
118 * Returns true if a plugin has already been registered on an instance.
120 * Plugin implementors are encouraged to use this first thing in a plugin
121 * to prevent the plugin from being called more than once on the same
125 * var base = new Base();
126 * base.use(function(app) {
127 * if (app.isRegistered('myPlugin')) return;
128 * // do stuff to `app`
131 * // to also record the plugin as being registered
132 * base.use(function(app) {
133 * if (app.isRegistered('myPlugin', true)) return;
134 * // do stuff to `app`
137 * @name .isRegistered
138 * @emits `plugin` Emits the name of the plugin being registered. Useful for unit tests, to ensure plugins are only registered once.
139 * @param {String} `name` The plugin name.
140 * @param {Boolean} `register` If the plugin if not already registered, to record it as being registered pass `true` as the second argument.
141 * @return {Boolean} Returns true if a plugin is already registered.
145 Base.prototype.isRegistered = function(name, register) {
146 if (this.registered.hasOwnProperty(name)) {
149 if (register !== false) {
150 this.registered[name] = true;
151 this.emit('plugin', name);
157 * Define a plugin function to be called immediately upon init. Plugins are chainable
158 * and expose the following arguments to the plugin function:
160 * - `app`: the current instance of `Base`
161 * - `base`: the [first ancestor instance](#base) of `Base`
164 * var app = new Base()
170 * @param {Function} `fn` plugin function to call
171 * @return {Object} Returns the item instance for chaining.
175 Base.prototype.use = function(fn) {
181 * The `.define` method is used for adding non-enumerable property on the instance.
182 * Dot-notation is **not supported** with `define`.
185 * // arbitrary `render` function using lodash `template`
186 * app.define('render', function(str, locals) {
187 * return _.template(str)(locals);
191 * @param {String} `key` The name of the property to define.
192 * @param {any} `value`
193 * @return {Object} Returns the instance for chaining.
197 Base.prototype.define = function(key, val) {
199 return this.visit('define', key);
201 define(this, key, val);
206 * Mix property `key` onto the Base prototype. If base is inherited using
207 * `Base.extend` this method will be overridden by a new `mixin` method that will
208 * only add properties to the prototype of the inheriting application.
211 * app.mixin('foo', function() {
216 * @param {String} `key`
217 * @param {Object|Array} `val`
218 * @return {Object} Returns the `base` instance for chaining.
222 Base.prototype.mixin = function(key, val) {
223 Base.prototype[key] = val;
228 * Non-enumberable mixin array, used by the static [Base.mixin]() method.
231 Base.prototype.mixins = Base.prototype.mixins || [];
234 * Getter/setter used when creating nested instances of `Base`, for storing a reference
235 * to the first ancestor instance. This works by setting an instance of `Base` on the `parent`
236 * property of a "child" instance. The `base` property defaults to the current instance if
237 * no `parent` property is defined.
240 * // create an instance of `Base`, this is our first ("base") instance
241 * var first = new Base();
242 * first.foo = 'bar'; // arbitrary property, to make it easier to see what's happening later
244 * // create another instance
245 * var second = new Base();
246 * // create a reference to the first instance (`first`)
247 * second.parent = first;
249 * // create another instance
250 * var third = new Base();
251 * // create a reference to the previous instance (`second`)
252 * // repeat this pattern every time a "child" instance is created
253 * third.parent = second;
255 * // we can always access the first instance using the `base` property
256 * console.log(first.base.foo);
258 * console.log(second.base.foo);
260 * console.log(third.base.foo);
262 * // and now you know how to get to third base ;)
268 Object.defineProperty(Base.prototype, 'base', {
271 return this.parent ? this.parent.base : this;
276 * Static method for adding global plugin functions that will
277 * be added to an instance when created.
280 * Base.use(function(app) {
283 * var app = new Base();
284 * console.log(app.foo);
288 * @param {Function} `fn` Plugin function to use on each instance.
289 * @return {Object} Returns the `Base` constructor for chaining
293 define(Base, 'use', function(fn) {
299 * Run an array of functions by passing each function
300 * to a method on the given object specified by the given property.
302 * @param {Object} `obj` Object containing method to use.
303 * @param {String} `prop` Name of the method on the object to use.
304 * @param {Array} `arr` Array of functions to pass to the method.
307 define(Base, 'run', function(obj, prop, arr) {
308 var len = arr.length, i = 0;
316 * Static method for inheriting the prototype and static methods of the `Base` class.
317 * This method greatly simplifies the process of creating inheritance-based applications.
318 * See [static-extend][] for more details.
321 * var extend = cu.extend(Parent);
322 * Parent.extend(Child);
324 * // optional methods
325 * Parent.extend(Child, {
326 * foo: function() {},
331 * @param {Function} `Ctor` constructor to extend
332 * @param {Object} `methods` Optional prototype properties to mix in.
333 * @return {Object} Returns the `Base` constructor for chaining
337 define(Base, 'extend', cu.extend(Base, function(Ctor, Parent) {
338 Ctor.prototype.mixins = Ctor.prototype.mixins || [];
340 define(Ctor, 'mixin', function(fn) {
341 var mixin = fn(Ctor.prototype, Ctor);
342 if (typeof mixin === 'function') {
343 Ctor.prototype.mixins.push(mixin);
348 define(Ctor, 'mixins', function(Child) {
349 Base.run(Child, 'mixin', Ctor.prototype.mixins);
353 Ctor.prototype.mixin = function(key, value) {
354 Ctor.prototype[key] = value;
361 * Used for adding methods to the `Base` prototype, and/or to the prototype of child instances.
362 * When a mixin function returns a function, the returned function is pushed onto the `.mixins`
363 * array, making it available to be used on inheriting classes whenever `Base.mixins()` is
364 * called (e.g. `Base.mixins(Child)`).
367 * Base.mixin(function(proto) {
368 * proto.foo = function(msg) {
369 * return 'foo ' + msg;
374 * @param {Function} `fn` Function to call
375 * @return {Object} Returns the `Base` constructor for chaining
379 define(Base, 'mixin', function(fn) {
380 var mixin = fn(Base.prototype, Base);
381 if (typeof mixin === 'function') {
382 Base.prototype.mixins.push(mixin);
388 * Static method for running global mixin functions against a child constructor.
389 * Mixins must be registered before calling this method.
392 * Base.extend(Child);
393 * Base.mixins(Child);
396 * @param {Function} `Child` Constructor function of a child class
397 * @return {Object} Returns the `Base` constructor for chaining
401 define(Base, 'mixins', function(Child) {
402 Base.run(Child, 'mixin', Base.prototype.mixins);
407 * Similar to `util.inherit`, but copies all static properties, prototype properties, and
408 * getters/setters from `Provider` to `Receiver`. See [class-utils][]{#inherit} for more details.
411 * Base.inherit(Foo, Bar);
414 * @param {Function} `Receiver` Receiving (child) constructor
415 * @param {Function} `Provider` Providing (parent) constructor
416 * @return {Object} Returns the `Base` constructor for chaining
420 define(Base, 'inherit', cu.inherit);
421 define(Base, 'bubble', cu.bubble);
426 * Expose `Base` with default settings
429 module.exports = namespace();
432 * Allow users to define a namespace
435 module.exports.namespace = namespace;