Created
February 5, 2017 05:59
-
-
Save miguel-leon/6cbbffad9dd68f9dfd2504c39468be65 to your computer and use it in GitHub Desktop.
angular.js decorator for $templateCache to add callbacks for when templates become registered
This file contains 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('$template-cache-watch', []) | |
.decorator('$templateCache', function ($delegate) { | |
var decorated = Object.create($delegate); | |
var callbacks = Object.create(null); | |
decorated.watch = function (key, callback) { | |
if (!callbacks[key]) { | |
callbacks[key] = []; | |
} | |
callbacks[key].push(callback); | |
}; | |
decorated.put = function (key, value) { | |
if ($delegate.put(key, value)) { | |
if (callbacks[key]) { | |
callbacks[key].forEach(function (callback) { | |
callback(value); | |
}); | |
delete callbacks[key]; | |
} | |
} | |
}; | |
decorated.remove = function (key) { | |
delete callbacks[key]; | |
$delegate.remove(key); | |
}; | |
decorated.removeAll = function (key) { | |
callbacks = Object.create(null); | |
$delegate.removeAll(key); | |
}; | |
return decorated; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment