Created
May 20, 2013 21:48
-
-
Save yocontra/5615848 to your computer and use it in GitHub Desktop.
External image loader for chrome apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just pass in the parent of the image tags. | |
# Image tags should have a .external-img class and data-src should be the external url | |
define -> | |
cache = {} | |
loader = ($el) -> | |
lazyLoad = (img) -> | |
el = $ img | |
url = el.attr 'data-src' | |
return el.attr('src', cache[url]) if cache[url]? | |
xhr = new XMLHttpRequest | |
xhr.open 'GET', url | |
xhr.responseType = 'blob' | |
xhr.onload = (e) -> | |
blob = window.webkitURL.createObjectURL @response | |
cache[url] = blob | |
el.attr 'src', blob | |
xhr.send() | |
imgs = $el.find(".external-img") | |
lazyLoad img for img in imgs | |
return $el | |
return loader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment