Skip to content

Instantly share code, notes, and snippets.

@justinobney
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinobney/2b4bdc9513e13e4a9852 to your computer and use it in GitHub Desktop.
Save justinobney/2b4bdc9513e13e4a9852 to your computer and use it in GitHub Desktop.
http-interceptor to add the location of a directives template.
app.config(function($provide, $httpProvider) {
labelDirectiveTemplates();
})
function labelDirectiveTemplates() {
$provide.decorator('$templateCache', function($delegate) {
var get = $delegate.get;
$delegate.get = decorateTemplateWithComment;
return $delegate;
function decorateTemplateWithComment(key) {
var value = get.call($delegate, key);
console.log('template::', key, value);
return addComment(key, value);
}
});
$httpProvider.interceptors.push(function($q) {
return {
'response': function(response) {
var url = response.config.url;
var isTemplate = url.indexOf('.html') > -1;
if (isTemplate) {
response.data = addComment(url, response.data);
}
return $q.when(response);
}
};
});
function addComment(url, template) {
if (!template)
return undefined;
if (angular.isArray(template))
template = template[1];
var commentBegin = '<!-- TEMPLATE-LOCATION: "';
var commentEnd = '" -->\n';
var comment = commentBegin + url + commentEnd;
if (template.indexOf(commentBegin) == -1)
return comment + template;
return template;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment