Ondrej Svejdar Ondrej Svejdar - 5 months ago 44
AngularJS Question

Override $templateCache to be case insensitive

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 ?

Answer

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) { normalGet(key.toLowerCase()); };
      templateCache.put = function(key,value) { normalPut(key.toLowerCase(), value); };

      return $delegate;
    }]);
}