-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamespace.js
More file actions
150 lines (130 loc) · 3.31 KB
/
Namespace.js
File metadata and controls
150 lines (130 loc) · 3.31 KB
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
146
147
148
149
150
/**
* Namespace constructor
*
* @param {Scope} scope Scope to which this namespace belongs
* @param {string} path Namespace path
* @returns {Namespace} Namespace class
*
* @constructor
*/
var Namespace = function(scope, path) {
/**
* Namespace class
*
* @typedef {function} Namespace
*
* @param {string} path Namespace path
* @returns {Namespace}
*/
var self = function(path /* injects.. , callback */) {
var namespace = self.getScope().getNamespace(self.adjustPath(path));
if (arguments.length > 1) {
namespace.space.apply(namespace, _.toArray(arguments).slice(1));
}
return namespace;
};
_.extend(self, Namespace.prototype);
self._scope = scope;
self._path = path;
self._spaces = [];
self._objects = {};
return self;
};
_.extend(Namespace.prototype, {
/**
* Gets namespace path
*
* @returns {string} Namespace path
*
* @this {Namespace}
*/
getPath: function() {
return this._path;
},
/**
* Gets namespace scope
*
* @returns {Scope} Namespace scope
*
* @this {Namespace}
*/
getScope: function() {
return this._scope;
},
/**
* Adjusts namespace path
*
* @param {string} path Namespace path
* @returns {string} Adjusted namespace path
*
* @see Scope::adjustPath()
*
* @this {Namespace}
*/
adjustPath: function(path) {
return this._scope.isAbsolutePath(path)
? this._scope.adjustPath(path)
: this._scope.concatPaths(this.getPath(), path);
},
/**
* Gets object assigned to namespace
* If object does not exist - namespace will try to create it by using factory method from scope
*
* @param {string} name Object name
* @returns {*} Object
*
* @this {Namespace}
*/
get: function(name) {
if (!(name in this._objects)) {
this._objects[name] = this._scope.get(name).call(this);
}
return this._objects[name];
},
/**
* Checks whether object with specified name exist
*
* @param {string} name Object name
* @returns {boolean} true if object with specified name exists
*
* @this {Namespace}
*/
has: function(name) {
return this._scope.has(name);
},
/**
* Add namespace callback (space)
*
* @returns {Namespace}
*
* @this {Namespace}
*/
space: function(/* injects.. , callback */) {
var self = this;
var injects = _.toArray(arguments).slice(0, -1);
var callback = _.last(arguments);
var objects = [];
if (!injects.length) {
injects = this._scope.getDefaultInjects();
}
for (var i = 0, ii = injects.length; i < ii; ++i) {
objects[i] = this.get(injects[i]);
}
this._spaces.push(function() {
callback.apply(self, objects);
});
return this;
},
/**
* Executes one space
*
* @returns {boolean} true if space was executed, false if there are no spaces
*/
executeSpace: function() {
if (!this._spaces.length) {
return false;
}
this._spaces.pop()();
return true;
}
});