forked from michaelbromley/angular-es6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregister.js
More file actions
142 lines (120 loc) · 4.42 KB
/
register.js
File metadata and controls
142 lines (120 loc) · 4.42 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
/**
* A helper class to simplify registering Angular components and provide a consistent syntax for doing so.
*/
function register(appName) {
var app = angular.module(appName);
return {
directive: directive,
controller: controller,
service: service,
provider: provider,
factory: factory
};
function directive(name, constructorFn) {
constructorFn = _normalizeConstructor(constructorFn);
if (!constructorFn.prototype.compile) {
// create an empty compile function if none was defined.
constructorFn.prototype.compile = () => {};
}
var originalCompileFn = _cloneFunction(constructorFn.prototype.compile);
// Decorate the compile method to automatically return the link method (if it exists)
// and bind it to the context of the constructor (so `this` works correctly).
// This gets around the problem of a non-lexical "this" which occurs when the directive class itself
// returns `this.link` from within the compile function.
_override(constructorFn.prototype, 'compile', function () {
return function () {
originalCompileFn.apply(this, arguments);
if (constructorFn.prototype.link) {
return constructorFn.prototype.link.bind(this);
}
};
});
var factoryArray = _createFactoryArray(constructorFn);
app.directive(name, factoryArray);
return this;
}
function controller(name, contructorFn) {
app.controller(name, contructorFn);
return this;
}
function service(name, contructorFn) {
app.service(name, contructorFn);
return this;
}
function provider(name, constructorFn) {
app.provider(name, constructorFn);
return this;
}
function factory(name, constructorFn) {
constructorFn = _normalizeConstructor(constructorFn);
var factoryArray = _createFactoryArray(constructorFn);
app.factory(name, factoryArray);
return this;
}
/**
* If the constructorFn is an array of type ['dep1', 'dep2', ..., constructor() {}]
* we need to pull out the array of dependencies and add it as an $inject property of the
* actual constructor function.
* @param input
* @returns {*}
* @private
*/
function _normalizeConstructor(input) {
var constructorFn;
if (input.constructor === Array) {
//
var injected = input.slice(0, input.length - 1);
constructorFn = input[input.length - 1];
constructorFn.$inject = injected;
} else {
constructorFn = input;
}
return constructorFn;
}
/**
* Convert a constructor function into a factory function which returns a new instance of that
* constructor, with the correct dependencies automatically injected as arguments.
*
* In order to inject the dependencies, they must be attached to the constructor function with the
* `$inject` property annotation.
*
* @param constructorFn
* @returns {Array.<T>}
* @private
*/
function _createFactoryArray(constructorFn) {
// get the array of dependencies that are needed by this component (as contained in the `$inject` array)
var args = constructorFn.$inject || [];
var factoryArray = args.slice(); // create a copy of the array
// The factoryArray uses Angular's array notation whereby each element of the array is the name of a
// dependency, and the final item is the factory function itself.
factoryArray.push((...args) => {
//return new constructorFn(...args);
var instance = new constructorFn(...args);
for (var key in instance) {
instance[key] = instance[key];
}
return instance;
});
return factoryArray;
}
/**
* Clone a function
* @param original
* @returns {Function}
*/
function _cloneFunction(original) {
return function() {
return original.apply(this, arguments);
};
}
/**
* Override an object's method with a new one specified by `callback`.
* @param object
* @param methodName
* @param callback
*/
function _override(object, methodName, callback) {
object[methodName] = callback(object[methodName])
}
}