Created
November 13, 2012 14:48
-
-
Save finnjohnsen/4066105 to your computer and use it in GitHub Desktop.
handlebars-src-loader.js
This file contains 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
/* | |
* Detects all scripts in DOM with src tag, and loads then into the html. | |
*/ | |
var HandlebarsSrcLoader = function() { | |
} | |
HandlebarsSrcLoader.prototype = { | |
load : function (callback) { | |
var thiz=this; | |
var callback=callback; | |
var loadProgressMap={}; | |
$('script[type="text/x-handlebars-template"][src]').each(function(index) { | |
var src = $(this).attr('src'); | |
var htmlElement = $(this).attr('id'); | |
loadProgressMap[htmlElement] = ''; //add the template to this progress map | |
$.get(src, {}, function (data) { | |
document.getElementById(htmlElement).text=data; //jQuery doesnt work in IE8. | |
delete loadProgressMap[htmlElement]; //the template is loaded, so remove it from the progress map | |
if (thiz.isEmpty(loadProgressMap) && thiz.isFunction(callback)) callback(); //if all templates are loaded, execute the callback to singal that we're finished. | |
}); | |
}); | |
} | |
, isFunction :function(subject) { | |
return (Object.prototype.toString.call(subject) == '[object Function]' ); | |
} | |
, isEmpty : function(map) { | |
var empty = true; | |
for(var key in map) { | |
empty = false; | |
break; | |
} | |
return empty; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment