Skip to content

Commit

Permalink
added success callback to useCachedFile (async method)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisben committed May 17, 2012
1 parent 37698c8 commit 203d97b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions js/imgcache.js
Expand Up @@ -229,7 +229,7 @@ var ImgCache = {
};

// $img: jQuery object of an <img/> element
ImgCache.useCachedFile = function($img, fail_callback) {
ImgCache.useCachedFile = function($img, success_callback, fail_callback) {

if (!ImgCache.filesystem || !ImgCache.dirEntry || !$img)
return;
Expand Down Expand Up @@ -259,17 +259,18 @@ var ImgCache = {
var base64content = e.target.result;
if (!base64content) {
logging('Error: file in cache ' + filename + ' is empty');
if (fail_callback) fail_callback();
if (fail_callback) fail_callback($img);
return;
}
_setNewImgPath($img, base64content, img_src);
logging('File ' + filename + ' loaded from cache');
if (success_callback) success_callback($img);
};
reader.readAsDataURL(file);
};
var _fail = function(error) {
logging('Error: Failed to read file ' + error.code);
if (fail_callback) fail_callback();
if (fail_callback) fail_callback($img);
};

entry.file(_win, _fail);
Expand All @@ -278,12 +279,13 @@ var ImgCache = {
var new_url = _getFileEntryURL(entry);
_setNewImgPath($img, new_url, img_src);
logging('File ' + filename + ' loaded from cache');
if (success_callback) success_callback($img);
}
};
// if file does not exist in cache, cache it now!
var _fail = function(e) {
logging('File ' + filename + ' not in cache');
if (fail_callback) fail_callback();
if (fail_callback) fail_callback($img);
};
ImgCache.dirEntry.getFile(filePath, { create: false }, _gotFileEntry, _fail);
}
Expand Down

0 comments on commit 203d97b

Please sign in to comment.