chrisben/imgcache.js — pull request #2 by dpnova (2012-07-20 18:35:46+00:00)
Title: Check if a particular image is already cached.

Hey, thanks for the very handy lib!

I wanted to have a bit more control over when images where cached. Particularly being able to cache them upon receiving some payload from the server on app startup. I didn't want to always download images I already had though. I added the function in this pull commit so I could do something like this (excuse the coffeescript)

```coffeescript
ImgCache.isCached = function(img_src, callback) {
path = _getCachedFilePath(img_src, ImgCache.dirEntry.fullPath);
errorHandler = function() {
callback(false);
};
return ImgCache.filesystem.root.getFile(path, {}, function(fileEntry) {
return fileEntry.file(function(file) {
var reader;
reader = new FileReader();
reader.onloadend = function() {
callback(true);
};
return reader.readAsText(file);
}, errorHandler);
}, errorHandler);
};
```