Last active
August 29, 2015 14:03
-
-
Save ogonkov/e9c0cedad0b9f27df092 to your computer and use it in GitHub Desktop.
Just a simple example of getting constructors from singleton.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Depends on Underscore `extend`. | |
*/ | |
/** | |
* @namespace app | |
*/ | |
window.app = app || {}; | |
/** | |
* @memberof app | |
* @namespace app.__ | |
*/ | |
app.__ = {}; | |
/** | |
* @param {Object} object | |
* @param {Number} start | |
* @param {Number=} end | |
* @returns {Array} | |
*/ | |
app.__.slice = function(object, start, end) { | |
var slice; | |
if (!start) { | |
start = 0; | |
} | |
slice = Array.prototype.slice; | |
return slice.call(object, start, end); | |
}; | |
/** | |
* Set module namespace and return new instance. | |
* | |
* @memberof app | |
* | |
* @param {String|Integer} id Module ID (i. e., UI.control.v3) | |
* @param {...String|Array=} elements Module elements by coma or array, default | |
* is 'models', 'collections', 'views'. | |
*/ | |
app.setModule = function(id, elements) { | |
var module; | |
module = id; | |
module = this.getModuleByName(id); | |
if (arguments.length > 2) { | |
elements = __.slice(arguments, 1); | |
} | |
if (elements === 'default' || !elements) { | |
elements = ['models', 'collections', 'views']; | |
} else if (typeof elements === 'string') { | |
elements = [elements]; | |
} | |
/** | |
* @private | |
* @param {String|Object} element | |
*/ | |
function setElement(element) { | |
if (typeof element === 'string' && | |
!module[element]) { | |
module[element] = {}; | |
} else if (typeof element) { | |
_.extend(module, element); | |
} | |
} | |
elements.forEach(setElement); | |
return module; | |
}; | |
/** | |
* Get instance by name | |
* | |
* @memberof app | |
* | |
* @param name | |
* @returns {Array|*} | |
*/ | |
app.getModuleByName = function(name) { | |
var module; | |
module = name.split('.'); | |
/** | |
* Resolve method in object | |
* If method not found -- return empty object. | |
* | |
* @private | |
* @param {String} method | |
* @param {Object} object | |
* @returns {*} | |
*/ | |
function resolve(method, object) { | |
var resolved; | |
if (!object[method]) { | |
resolved = object[method] = {}; | |
} else { | |
resolved = object[method]; | |
} | |
return resolved; | |
} | |
/** | |
* @this app | |
* @private | |
* @param {String} id | |
* @param {Integer} i | |
*/ | |
function lookup(id, i) { | |
if (i > 0) { | |
module = resolve(id, module); | |
} else { | |
module = resolve(id, this); | |
} | |
} | |
if (module.length >= 2) { | |
module.concat().forEach(lookup, this); | |
} else { | |
module = resolve(name, this); | |
} | |
return module; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment