Skip to content

Commit 2d495bb

Browse files
committed
added callback function & doublecheck double files
1 parent ad339a5 commit 2d495bb

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ This method is preferred for bigger images, as this can happen in the background
5353
require('To.ImageCache').cache('http://example.com/image.jpg');
5454
```
5555

56-
This function will NOT return a blob, but will cache the file using `XHR`.
56+
This function will NOT return a blob, but will cache the file using `XHR`.
57+
58+
Aditonally, you can add a timeout and callback function:
59+
60+
```js
61+
require('To.ImageCache').cache('http://example.com/image.jpg', 25000, function(blob){
62+
$.imageView.image = blob;
63+
});
64+
```
5765

5866
## Clearing Cache
5967

To.ImageCache/0.1/To.ImageCache.js renamed to To.ImageCache/0.1.1/To.ImageCache.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ var storeFile = function(filename, blob){
150150
// check if directory has been created
151151
checkDir();
152152

153+
// we already have this file
154+
if (hasFile(filename)){
155+
blob = null;
156+
return;
157+
}
158+
153159
var path = Ti.Filesystem.applicationDataDirectory + c.folder;
154160
var file = Ti.Filesystem.getFile(path, filename);
155161
file.write(blob);
@@ -231,9 +237,10 @@ var remoteImage = function(url){
231237
* This function will fetch the image in the background
232238
* with a configurable cache period
233239
* @param {String} url of the image to cache
234-
* @param {Integer} Timeout in milliseconds
240+
* @param {Integer} (Optional) Timeout in milliseconds
241+
* @param {Function} (Optional) callback function, blob will be returned
235242
*/
236-
var cache = function(url, timeout){
243+
var cache = function(url, timeout, cb){
237244
var timeout = timeout || 30000;
238245

239246
// if file is already cached, don't do so again
@@ -248,6 +255,7 @@ var cache = function(url, timeout){
248255
var xhr = Titanium.Network.createHTTPClient({
249256
onload: function() {
250257
storeFile(filename, this.responseData);
258+
cb && cb(readFile(filename));
251259
},
252260
timeout: timeout
253261
});

To.ImageCache/0.1/manifest renamed to To.ImageCache/0.1.1/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 0.1
1+
version: 0.1.1
22
description: A simple Commonjs module to cache images a little less temporarily
33
author: Rene Pot
44
license: Apache 2.0

dist/To.ImageCache-commonjs-0.1.1.zip

5.41 KB
Binary file not shown.

0 commit comments

Comments
 (0)