Skip to content

Instantly share code, notes, and snippets.

@fwahlqvist
Last active August 29, 2015 14:11
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 fwahlqvist/08bd64852da06197d33d to your computer and use it in GitHub Desktop.
Save fwahlqvist/08bd64852da06197d33d to your computer and use it in GitHub Desktop.
angular.module('app.core')
.factory('templateModifierInterceptor', [
'$q',
'$templateCache',
function($q) {
var modifiedTemplates = {};
var HAS_FLAGS_REGEX = /data-dom-(remove|keep)/;
var HTML_PAGE_REGEX = /\.html$|\.html\?/i;
return {
response: function(response) {
var url = response.config.url,
responseData = response.data;
if(!modifiedTemplates[url] && HTML_PAGE_REGEX.test(url) && HAS_FLAGS_REGEX.test(responseData)) {
var template = $('<div>').append(responseData);
// Find and parse the keep/omit attributes in the view.
template.find('[data-dom-keep],[data-dom-remove]').each(function() {
var removeElement;
var element = $(this);
var data = element.data();
var removeFlags = data.domRemove;
var keepFlags = data.domKeep;
//todo: implement your own logic for removing elements from the template
//example implementation where the data attribute stores user permissions
/*var flags;
if(removeFlags !== undefined) {
removeElement = false;
flags = removeFlags.split(',');
} else {
removeElement = true;
flags = removeFlags.split(',');
}
if(session.user.hasPermissions(flags)) {
removeElement = !removeElement;
}*/
if(removeElement === true) {
element.remove();
}
});
//update template cache
response.data = template.html();
$templateCache.put(url, response.data);
modifiedTemplates[url] = true;
}
return response;
},
responseError: function(response) {
return $q.reject(response);
}
};
}
])
.config(['$httpProvider',function($httpProvider) {
$httpProvider.interceptors.push('templateModifierInterceptor');
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment