forked from andrewmcgivery/ionic-ion-imageCacheFactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathionic.ion.imagecachefactory.js
34 lines (27 loc) · 1003 Bytes
/
ionic.ion.imagecachefactory.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
angular.module('ionic.ion.imageCacheFactory', [])
.factory('$ImageCacheFactory', ['$q', function($q) {
return {
Cache: function(urls) {
if (!(urls instanceof Array))
return $q.reject('Input is not an array');
var promises = [];
for (var i = 0; i < urls.length; i++) {
var deferred = $q.defer();
var img = new Image();
img.onload = (function(deferred) {
return function(){
deferred.resolve();
}
})(deferred);
img.onerror = (function(deferred,url) {
return function(){
deferred.reject(url);
}
})(deferred,urls[i]);
promises.push(deferred.promise);
img.src = urls[i];
}
return $q.all(promises);
}
}
}]);