Permalink
Browse files

added success callback to useCachedFile (async method)

  • Loading branch information...
1 parent 37698c8 commit 203d97b1aa58a8982373bea60cc7eab211e5aa00 @chrisben committed May 17, 2012
Showing with 6 additions and 4 deletions.
  1. +6 −4 js/imgcache.js
View
10 js/imgcache.js
@@ -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;
@@ -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);
@@ -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);
}

0 comments on commit 203d97b

Please sign in to comment.