Last active
August 29, 2015 13:57
-
-
Save maxbates/9655719 to your computer and use it in GitHub Desktop.
Override the angular script directive (used for templates) with this if you need to use dynamic (e.g. interpolated or put through angular function) URLs in dynamically loaded content
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
angular.module('myApp') | |
.run(function ($rootScope) { | |
$rootScope.mixin = function(urls) { | |
if (angular.isUndefined(urls) || urls == '') { | |
return $q.when('no mixin url'); | |
} | |
var deferred = $q.defer(); | |
//timeout our requests at 5 seconds | |
var timeoutPromise = $timeout(function() { deferred.reject(null) }, 5000); | |
//assume that $script or some other way of downloading scripts is present | |
$script(urls, function() { | |
$timeout.cancel(timeoutPromise); | |
$rootScope.$safeApply(deferred.resolve(urls)); | |
}); | |
return deferred.promise; | |
}; | |
$document.on('WidgetContentLoaded', function () { | |
//put more interesting logic here... this is like $(document).ready() but for your included partial | |
console.log('yay we loaded your scripts'); | |
}); | |
}) | |
.service('lazyScripts', ['$q', '$timeout', '$document', function ($q, $timeout, $document) { | |
var promises = []; | |
this.register = function (url) { | |
promises.push($clotho.extensions.mixin(url)); | |
}; | |
$timeout(function() { | |
$q.all(promises).then(function() { | |
//broadcast event | |
$document.triggerHandler('WidgetContentLoaded'); | |
}) | |
}); | |
}]) | |
.directive('script', function($parse, $rootScope) { | |
return { | |
restrict: 'E', | |
terminal: true, | |
compile: function(element, attr) { | |
if (attr.ngSrc) { | |
var scriptUrl = $parse(attr.ngSrc)($rootScope); | |
lazyScripts.register(scriptUrl); | |
} | |
} | |
}; | |
}); |
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
<script type="application/javascript"> | |
/* | |
Angular provides was to dynamically load templates with dynamic names via ng-include. | |
We needed to download scripts, relative to the path of the .html partial calling them. | |
(i.e. we had a directory with a file to include, and wanted the .html file to declare the scripts etc. it needed itself). | |
Inline JS and CSS will load fine, but not scripts with src like the one below. | |
This directive will patch angular so that you can use ng-src and pass in an interpolated src, or one generated by a function. | |
Here, we are assuming that prefixUrl() is a method on the $rootScope (or accessible some other way) | |
For example, prefixUrl('x.js') may generate 'namespaced/path/x.js' | |
You can extend the functionality of the message broadcasted above to only run after all tags are downloaded... Just register all the scripts you need to download and emit the message after $q.all() | |
This assumes you have some way of downloading scripts dynamically. We are using $script, but jquery or whatever would work too. | |
*/ | |
</script> | |
<script type="application/javascript" ng-src="prefixUrl('inlineCalledScript.js')"></script> | |
<style type="text/css"> | |
.greenListItem { | |
color: #44bb44; | |
} | |
</style> | |
<ul> | |
<li>This is a dynamically loaded template.</li> | |
<li>Note that angular must already be bootstrapped, with the new script directive above. This will not work in your index.html file</li> | |
<li class="greenListItem">Inline CSS works!</li> | |
</ul> | |
<!-- this would work without problems --> | |
<div ng-include="prefixUrl('anotherPartial.html')"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment