-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocky.js
145 lines (116 loc) · 4.17 KB
/
ocky.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* jshint -W106 */
(function(window) {
'use strict';
// Ocky
// ----
// A simple module system.
// Provides privacy and encapsultations for modules in an application.
var Ocky = function(moduleName, definition, parent) {
if (!parent) {
moduleName = moduleName || 'root';
if (!moduleName.match(/^[a-zA-Z0-9]+$/)) {
throw new Error('Cannot create a sub-module without a parent module');
}
}
this.moduleName = moduleName;
this.subModules = {};
this.parent = parent;
this.moduleClass = this.moduleClass || this.constructor;
this.initialize(moduleName, definition, parent);
if (definition && _.isFunction(definition)) {
this.addDefinition(definition);
}
};
_.extend(Ocky.prototype, {
/**
* Empty initialize method that can be used by classes that extend Ocky
* This is executed during module instantation, before it has any definition.
*/
initialize: function() {},
/**
* Splits the full module name into namespace components
*/
_processName: function(moduleNames) {
return moduleNames.split('.');
},
/**
* Create a submoule with an optional definition.
*
* The name will automatically create namespaces so
* you can create a module a few levels down easily.
*
* Additional arguments will be passed into the definition
* function as dependencies.
*
* `MyModule.module('SomeChild', function(SomeChild, Marionette) {}, Backbone.Marionette);`
*/
module: function(name, definition) {
var names = this._processName(name);
var parent = this;
var submodule;
var dependencies = _.rest(arguments, 2);
// nested children to make
if (names.length > 1) {
_.each(names, function(name) {
parent = parent.module(name);
}, this);
submodule = parent;
} else if (names.length === 1) {
// single child to make
if (!_.has(this.subModules, name)) {
this[name] = this.subModules[name] = this.createSubmodule(name);
}
submodule = this.subModules[name];
}
if (definition && _.isFunction(definition)) {
submodule.addDefinition(definition, dependencies);
}
return submodule;
},
// Initialize the submodule, including a reference to this parent module
createSubmodule: function(name, definition) {
var ModuleClass = this.moduleClass;
return new ModuleClass(name, definition, this);
},
// Run a definition function against this module
// You can pass an optional array of dependencies
addDefinition: function(definition, dependencies) {
var inject = [this].concat(dependencies);
definition.apply(this, inject);
}
});
// Extend helper (ripped from Backbone)
// -------
// Helper function to correctly set up the prototype chain, for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
Ocky.extend = function(protoProps, staticProps) {
var parent = this;
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}
// Add static properties to the constructor function, if supplied.
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate();
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) {
_.extend(child.prototype, protoProps);
}
// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;
return child;
};
window.Ocky = Ocky;
}(window));