Skip to content

Instantly share code, notes, and snippets.

@klebba
Created August 22, 2013 22:33
Show Gist options
  • Save klebba/6313636 to your computer and use it in GitHub Desktop.
Save klebba/6313636 to your computer and use it in GitHub Desktop.
Transparently intercept requests for templates that are known to rails and rewrite the URL
angular.module('templates', []).config(['$httpProvider', function($httpProvider) {
/* Injector to rewrite template filenames like template.html to
rails asset paths like /assets/template-0def1e14f3772cda8a0110f73fb6beb1.html */
$httpProvider.interceptors.push(['$cacheFactory', '$sce', function($cacheFactory, $sce) {
var cache = $cacheFactory('templatesMap');
/* Rails derives this list automatically */
// <% Dir.glob(Rails.root.join('app','assets','templates', '*.html')).each do |f| file = File.basename(f) %>
cache.put('<%= file %>', '<%= asset_path file %>');
// <% end %>
return {
request: function(config) {
var templateUrl = cache.get(config.url);
if(templateUrl) {
config.url = $sce.getTrustedResourceUrl(templateUrl);
}
return config;
}
};
}]);
}]);
@klebba
Copy link
Author

klebba commented Aug 22, 2013

Tested in Angular 1.2.0rc1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment