Skip to content

Instantly share code, notes, and snippets.

@miguel-leon
Created February 5, 2017 05:59
Show Gist options
  • Save miguel-leon/6cbbffad9dd68f9dfd2504c39468be65 to your computer and use it in GitHub Desktop.
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
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