Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save nikhilkesari/5bdcc142189b711ba9af81da96deb399 to your computer and use it in GitHub Desktop.

Select an option

Save nikhilkesari/5bdcc142189b711ba9af81da96deb399 to your computer and use it in GitHub Desktop.

Revisions

  1. @idosela idosela revised this gist Jan 15, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions http-response-interceptor.js
    Original file line number Diff line number Diff line change
    @@ -28,10 +28,10 @@ angular.module('myMdl', []).config(['$httpProvider', function($httpProvider) {
    var element = $(this),
    data = element.data(),
    keep = data.keep,
    features = parseFeatures(keep || data.omit || '');
    features = keep || data.omit || '';

    // Check if the user has all of the specified features.
    var hasFeature = _.all(features, function(feature) {
    var hasFeature = _.all(features.split(','), function(feature) {
    return activeProfile.features[feature];
    });

  2. @idosela idosela revised this gist Jan 14, 2014. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions http-response-interceptor.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    angular.module('myMdl', []).config(['$httpProvider', function($httpProvider) {
    $httpProvider.responseInterceptors.push(['$q', '$templateCache',
    function($q, $templateCache) {
    $httpProvider.responseInterceptors.push([
    '$q', '$templateCache', 'activeProfile',
    function($q, $templateCache, activeProfile) {

    // Keep track which HTML templates have already been modified.
    var modifiedTemplates = {};
    @@ -31,7 +32,7 @@ angular.module('myMdl', []).config(['$httpProvider', function($httpProvider) {

    // Check if the user has all of the specified features.
    var hasFeature = _.all(features, function(feature) {
    return profile.features[feature];
    return activeProfile.features[feature];
    });

    if (flags.features.length &&
  3. @idosela idosela created this gist Jan 14, 2014.
    61 changes: 61 additions & 0 deletions http-response-interceptor.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    angular.module('myMdl', []).config(['$httpProvider', function($httpProvider) {
    $httpProvider.responseInterceptors.push(['$q', '$templateCache',
    function($q, $templateCache) {

    // Keep track which HTML templates have already been modified.
    var modifiedTemplates = {};

    // Tests if there are any keep/omit attributes.
    var HAS_FLAGS_EXP = /data-(keep|omit)/;

    // Tests if the requested url is a html page.
    var IS_HTML_PAGE = /\.html$|\.html\?/i;

    return function(promise) {
    return promise.then(function(response) {
    var url = response.config.url,
    responseData = response.data;

    if (!modifiedTemplates[url] && IS_HTML_PAGE.test(url) &&
    HAS_FLAGS_EXP.test(responseData)) {

    // Create a DOM fragment from the response HTML.
    var template = $('<div>').append(responseData);

    // Find and parse the keep/omit attributes in the view.
    template.find('[data-keep],[data-omit]').each(function() {
    var element = $(this),
    data = element.data(),
    keep = data.keep,
    features = parseFeatures(keep || data.omit || '');

    // Check if the user has all of the specified features.
    var hasFeature = _.all(features, function(feature) {
    return profile.features[feature];
    });

    if (flags.features.length &&
    ((keep && !hasFeature) || (!keep && hasFeature))) {
    element.remove();
    }
    });

    // Set the modified template.
    response.data = template.html();

    // Replace the template in the template cache, and mark the
    // template as modified.
    $templateCache.put(url, response.data);
    modifiedTemplates[url] = true;
    }

    return response;
    },

    // Reject the response in case of an error.
    function(response) {
    return $q.reject(response);
    });
    };
    }]);
    }]);