-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Update cacheFactory.js: Added peek() for returning all keys #3760
Conversation
Added the peek() for returning all keys.
fixed the broken code.
Added unit test for peek()
Updated the doc.
Fixed the matcher.
Updated the test.
Check out AngularCache.keys() or AngularCache.keySet(), plus more goodies! |
I'm sorry, but I wasn't able to verify your CLA signature. CLA signature is required for any code contributions to AngularJS. Please sign our CLA and ensure that the CLA signature email address and the email address in this PR's commits match. If you signed the CLA as a corporation, please let me know the company's name. Thanks a bunch! PS: If you signed the CLA in the past then most likely the email addresses don't match. Please sign the CLA again or update the email address in the commit of this PR. |
Just signed. |
This never got merged, is there any plan to add exposure to the keys in the cache? Can be useful. |
Wow this would be amazing to have, +1 for merge! |
02dc2aa
to
fd2d6c0
Compare
cad9560
to
f294244
Compare
e8dc429
to
e83fab9
Compare
4dd5a20
to
998c61c
Compare
This is a very useful little change. I had a major issue getting Karma tests for htmlTemplates in directives to work properly with ngHtml2JsPreprocessor because the key's it was generating were not matching the GET call, and I could only tell from .info() that the data was in the $templateCache not what the available keys were. I hacked this into my distro so I could peek() the keys and fixed my problem in minutes. Thanks zsong! |
Can we get this in? |
This appears abandoned. Plus, the commits do not follow the contribution guidelines so there's still work that needs to be done. Plus a rebase. |
Is anything happening with this? |
You can replace Angular's It's a point of discussion, but I think certain parts of Angular should be as lightweight as possible (or removed entirely). You then bring in various libraries (like angular-cache) that are really good at what they do to augment your app, so Angular doesn't have to be a slow-moving behemoth. |
I use this: app.config(['$provide', function($provide) {
// monkey-patches $templateCache to have a keys() method
$provide.decorator('$templateCache', [
'$delegate', function($delegate) {
var keys = [], origPut = $delegate.put;
$delegate.put = function(key, value) {
origPut(key, value);
keys.push(key);
};
// we would need cache.peek() to get all keys from $templateCache, but this features was never
// integrated into Angular: https://github.com/angular/angular.js/pull/3760
// please note: this is not feature complete, removing templates is NOT considered
$delegate.getKeys = function() {
return keys;
};
return $delegate;
}
]);
}]); |
I agree with @jmdobry here. Please use custom implementations of caching, such as https://github.com/jmdobry/angular-cache, or roll your own, as @JohannesHoppe has done. |
I just implemented @JohannesHoppe idea on https://github.com/DinisCruz/Maturity-Models using coffee script. Here is the code and tests Patches.coffee angular.module('MM_Graph')
.config ($provide)->
$provide.decorator '$templateCache', ($delegate)-> # adds a keys() method to $templateCache
# (based on http://stackoverflow.com/a/37834469/262379)
keys = [] # variable to store keys added
origPut = $delegate.put # capture the original put method
$delegate.put = (key, value)-> # replaced it with our own
origPut(key, value) # call original
keys.push(key) # store value in keys variable
$delegate.get_Keys = ()-> keys # new method to return keys
return $delegate Patches.test.coffee describe '| angular | Patches ', ->
beforeEach ()->
module('MM_Graph')
it '$templateCache.get_Keys',->
inject ($templateCache)->
test_Key = 'an_Key'
test_Value = 'an_Value'
using $templateCache, ()->
@.keys().assert_Is [ 'put' , 'get' , 'remove', 'removeAll',
'destroy', 'info' , 'get_Keys' ] # confirm method was added to object
@.get_Keys().size().assert_Is $templateCache.info().size # check get_Keys size
@.get_Keys().assert_Not_Contains test_Key # confirm test key is not there
@.put test_Key, test_Value # add a new key/value pair
@.get_Keys().assert_Contains test_Key # confirm that test key is now there
@.get(test_Key).assert_Is test_Value # confirm that test value is now mapped to test key
@.get_Keys().size().assert_Is $templateCache.info().size # double check that this still works ok |
You also need to patch |
I don't know if anyone is interested in this anymore, but I have update the code @JohannesHoppe for a
|
Thanks for the solution @JohannesHoppe |
Hey guys, you should all switch to Angular 2/4 now! ;-) |
AngularJS 1.7+ requires template cache implementation to return template value in put() since fb00991 New function definition would be ` $provide.decorator('$templateCache', [
|
Added peek() for returning all keys.