-
-
Save justinobney/2b4bdc9513e13e4a9852 to your computer and use it in GitHub Desktop.
http-interceptor to add the location of a directives template.
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
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