-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollerTrainer.js
66 lines (57 loc) · 2.21 KB
/
controllerTrainer.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
/**
* Decorates $controller with a patching function to
* throw an error if DOM APIs are manipulated from
* within an Angular controller
*/
angular.module('controllerTrainer', []).
config(function ($provide) {
$provide.decorator('$controller', function($delegate, $injector) {
//Implemented within a zone from zone.js in order
//to account for asynchronous controller manipulation
var controllerZone = zone.fork({
beforeTask: function() {
//Patches prototypes and individual HTML elements
//that represent the DOM APIs.
//Takes an optional parameter of a function to use as the
//error indicator. Without the parameter patchServices.listener() is
//used as the default patching function.
patchServices.addManipulationListener();
},
afterTask: function() {
//Removes the patching of prototypes and
//individual HTML elements
patchServices.removeManipulationListener();
}
});
var patchedServices = {};
return function(ctrl, locals) {
var dependencies = $injector.annotate(ctrl);
// patch methods on $scope
if (!locals) {
locals = {};
}
if (locals.$scope) {
Object.keys(locals.$scope).forEach(function (prop) {
if (prop[0] !== '$' && typeof locals.$scope[prop] === 'function') {
locals.$scope[prop] = controllerZone.bind(locals.$scope[prop]);
}
});
}
if (dependencies.indexOf('$timeout') > -1) {
locals.$timeout = patchedServices.$timeout =
(patchedServices.$timeout || controllerZone.bind($injector.get('$timeout')));
}
// body of controller
patchServices.addManipulationListener();
var ctrlInstance = $delegate.apply(this, [ctrl, locals]);
patchServices.removeManipulationListener();
// controller.test
Object.keys(ctrlInstance).forEach(function (prop) {
if (prop[0] !== '$' && typeof ctrlInstance[prop] === 'function') {
ctrlInstance[prop] = controllerZone.bind(ctrlInstance[prop]);
}
});
return ctrlInstance;
};
});
});