Skip to content

Commit

Permalink
update cacheImg()
Browse files Browse the repository at this point in the history
  • Loading branch information
tower1229 committed Jul 10, 2018
1 parent 6a03b7d commit c2d730c
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions sdk/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,39 +373,36 @@ define(function(require, exports, module) {
};

var cacheImg = function(element, callback) {
var placeholderPic = seajs.root + '/res/img/placeholder.jpg';
var remoteEle;
var cacheCount = 0;
var setImg = function(ele, url){
if (ele.tagName.toLowerCase() === 'img') {
ele.setAttribute('src', url);
} else {
ele.style.backgroundImage = "url(" + url + ")";
}
ele.removeAttribute('data-remote');
cacheCount++;
if (cacheCount === remoteEle.length) {
typeof callback === 'function' && callback();
}
};
if ($(element)[0].getAttribute('data-remote')) {
remoteEle = $(element);
} else {
remoteEle = $(element)[0].querySelectorAll('[data-remote]');
}
app.ready(function() {
var cacheCount = 0;
$.each(remoteEle, function(i, ele) {
var remote = ele.getAttribute('data-remote');
if(remote){
api.imageCache({
url: remote,
policy: "cache_else_network"
url: remote
}, function(ret, err) {
var url = ret.url;
setImg(ele, url);
ele.removeAttribute('data-remote');
cacheCount++;
if (cacheCount === remoteEle.length) {
typeof callback === 'function' && callback();
}
setImg(ele, ret.url);
});
}else{
setImg(ele, placeholderPic);
setImg(ele, remote);
}
});
});
Expand Down

0 comments on commit c2d730c

Please sign in to comment.