Repository navigation
Expand file tree
/
Copy pathangular-pxy.js
More file actions
57 lines (50 loc) · 1.93 KB
/
Copy pathangular-pxy.js
File metadata and controls
57 lines (50 loc) · 1.93 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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([ 'module', 'angular', 'pxy' ], function (module, angular, Pxy) {
module.exports = factory(angular, Pxy);
});
} else if (typeof module === 'object') {
module.exports = factory(require('angular'), require('pxy'));
} else {
if (!root.mp) {
root.mp = {};
}
root.mp.pxy = factory(root.angular, Pxy);
}
}(this, function (angular, Pxy) {
'use strict';
return angular.module('mp.pxy', [])
.provider('$pxy', function () {
this.onCancelPromise = undefined;
var provider = this;
this.$get = [ '$q', '$timeout', '$interval', function PxyFactory($q, $timeout, $interval) {
return {
create: function () {
return new Pxy($q, function (promise) {
if (provider.onCancelPromise && provider.onCancelPromise(promise)) {
return;
}
if (promise.$$timeoutId) {
$timeout.cancel(promise);
} else if (promise.$$intervalId) {
$interval.cancel(promise);
}
});
}
};
}];
})
.run([ '$rootScope', '$pxy', function ($rootScope, $pxy) {
Object.getPrototypeOf($rootScope).$pxy = function (promise) {
var scope = this,
pxy;
if (!scope.hasOwnProperty('$$pxy')) {
pxy = scope.$$pxy = $pxy.create();
scope.$on('$destroy', pxy.invalidate);
} else {
pxy = scope.$$pxy;
}
return pxy.proxy(promise);
};
}]);
}));