Last active
August 29, 2015 14:11
-
-
Save fwahlqvist/08bd64852da06197d33d to your computer and use it in GitHub Desktop.
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('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