Created
August 22, 2013 22:33
-
-
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
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
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; | |
} | |
}; | |
}]); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested in Angular 1.2.0rc1