Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override $templateCache to be case insensitive

Tags:

Can one override core provider like $templateCache while maintaining reference to the original provider ? I'd like to override $templateCache to be case insensitive.

I.E. something like

var normalGet = $templateCache.get;
var normalPut = $templateCache.put;
$templateCache.get = function(key) { normalGet(key.toLowerCase()); };
$templateCache.put = function(key,value) { normalPut(key.toLowerCase(), value); };

But less hacky, more DI-style ?

like image 805
Ondrej Svejdar Avatar asked Jun 28 '16 14:06

Ondrej Svejdar


1 Answers

I'd say use decorator to modify the actual Provider code which will be done at configuration phase before coming into action.

We used $templateCacheProvider because Provider appended prefix indicate that its provider(it can be Directive when you are modifying directive DDO of directive). You have to place this code inside config phase of your application.

Code

app.config(['$provide', Decorate]);
function Decorate($provide) {
  $provide.decorator('$templateCacheProvider', 
    ['$delegate', function($delegate) {
      var templateCache = $delegate[0];

      var normalGet = templateCache.get;
      var normalPut = templateCache.put;
      templateCache.get = function(key) { return normalGet(key.toLowerCase()); };
      templateCache.put = function(key,value) { normalPut(key.toLowerCase(), value); };

      return $delegate;
    }]);
}
like image 183
Pankaj Parkar Avatar answered Sep 28 '22 03:09

Pankaj Parkar