Skip to content

Instantly share code, notes, and snippets.

@mordendk
Created March 26, 2014 15:40
Show Gist options
  • Save mordendk/9786276 to your computer and use it in GitHub Desktop.
Save mordendk/9786276 to your computer and use it in GitHub Desktop.
'use strict';
angular
.module('common.d3')
.provider('common.d3.loader', function() {
// Default to CDN
this.scriptUrl = 'http://d3js.org/d3.v3.min.js';
this.$get = ['$window', '$document', '$q', function($window, $document, $q) {
var defer = $q.defer(),
scriptUrl = this.scriptUrl;
return {
done: function(){
var onScriptLoad = function(){
return defer.resolve($window.d3);
};
if($window.d3){
onScriptLoad();
}
else{
var scriptTag = $document[0].createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.async = true;
scriptTag.src = scriptUrl;
scriptTag.onreadystatechange = function () {
if (this.readyState === 'complete') {
onScriptLoad();
}else{
defer.reject();
}
};
scriptTag.onload = onScriptLoad;
var s = $document[0].getElementsByTagName('body')[0];
s.appendChild(scriptTag);
}
return defer.promise;
}
};
}];
this.setScriptUrl = function(url) {
this.scriptUrl = url;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment