Skip to content

Instantly share code, notes, and snippets.

@psyho
Created August 24, 2011 17:55
Show Gist options
  • Save psyho/1168690 to your computer and use it in GitHub Desktop.
Save psyho/1168690 to your computer and use it in GitHub Desktop.
caching partials in angular
<!doctype html>
<html xmlns:ng="http://angularjs.org">
<script src="http://code.angularjs.org/angular-0.9.19.min.js" ng:autobind></script>
<script type="text/javascript">
// this is the service generated by partials.rb
angular.service('partials', function() {
return {
'/foo': 'Foo template',
'/bar': 'Bar template'
};
});
angular.service('partialLoader', function(partials, xhrCache) {
angular.forEach(partials, function(content, path) {
xhrCache.data[path] = {value: content, headers: {}};
});
return true;
}, {$inject: ['partials', '$xhr.cache'], $eager: true});
</script>
<body>
<p>/foo</p>
<ng:include src="'/foo'"></ng:include>
<p>/bar</p>
<ng:include src="'/bar'"></ng:include>
</body>
</html>
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
partial_directory = ARGV[0]
unless partial_directory
puts "Usage: partials.rb directory"
exit(1)
end
files = Dir["#{partial_directory}/**/*.html"]
partials = files.map{ |file| [file.gsub(/^#{partial_directory}/, ''), File.read(file)] }
puts <<-JS
angular.service('partials', function() {
return #{Hash[partials].to_json};
});
JS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment