Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Update cacheFactory.js: Added peek() for returning all keys #3760

Closed
wants to merge 6 commits into from
Closed

Update cacheFactory.js: Added peek() for returning all keys #3760

wants to merge 6 commits into from

Conversation

zsong
Copy link
Contributor

@zsong zsong commented Aug 27, 2013

Added peek() for returning all keys.

zsong added 6 commits August 27, 2013 00:57
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.
@jmdobry
Copy link

jmdobry commented Oct 12, 2013

Check out AngularCache.keys() or AngularCache.keySet(), plus more goodies!

@IgorMinar
Copy link
Contributor

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.
PS2: If you are a Googler, please sign the CLA as well to simplify the CLA verification process.

@zsong
Copy link
Contributor Author

zsong commented Dec 11, 2013

Just signed.
Ziang Song

@atticoos
Copy link

This never got merged, is there any plan to add exposure to the keys in the cache? Can be useful.

@Tomtomgo
Copy link

Wow this would be amazing to have, +1 for merge!

@blackenedwings
Copy link

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!

@eranimo
Copy link

eranimo commented Feb 5, 2015

Can we get this in?

@atticoos
Copy link

atticoos commented Feb 5, 2015

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.

@axschech
Copy link

axschech commented Jul 7, 2015

Is anything happening with this?

@jmdobry
Copy link

jmdobry commented Jul 7, 2015

You can replace Angular's $cacheFactory with angular-cache instead, and get goodies like this right now.

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.

@JohannesHoppe
Copy link

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;
        }
    ]);
}]);

@petebacondarwin
Copy link
Contributor

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.

@DinisCruz
Copy link

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

@gkalpak
Copy link
Member

gkalpak commented Jun 15, 2016

You also need to patch remove fwiw.

@maniakill
Copy link

maniakill commented Nov 23, 2016

I don't know if anyone is interested in this anymore, but I have update the code @JohannesHoppe for a remove method also.
` $provide.decorator('$templateCache', [

        '$delegate', function($delegate) {

            var keys = [], origPut = $delegate.put, origRem = $delegate.remove;

            $delegate.put = function(key, value) {
                origPut(key, value);
                if(keys.indexOf(key) === -1){
	                keys.push(key);
	            }else{
	            	keys[key]= value;
	            }
            };

            $delegate.remove = function(key) {
                origRem(key);
                keys.splice(keys.indexOf(key),1);
            };

           

            $delegate.getKeys = function() {
                return keys;
            };

            return $delegate;
        }
    ]);`

@helloanoop
Copy link

Thanks for the solution @JohannesHoppe

@JohannesHoppe
Copy link

Hey guys, you should all switch to Angular 2/4 now! ;-)

@logist
Copy link

logist commented Jan 18, 2019

AngularJS 1.7+ requires template cache implementation to return template value in put() since fb00991

New function definition would be

` $provide.decorator('$templateCache', [

        '$delegate', function($delegate) {

            var keys = [], origPut = $delegate.put, origRem = $delegate.remove;

            $delegate.put = function(key, value) {
                if(keys.indexOf(key) === -1){
	                keys.push(key);
	            }else{
	            	keys[key]= value;
	            }
                return origPut(key, value);
            };

            $delegate.remove = function(key) {
                keys.splice(keys.indexOf(key),1);
                return origRem(key);
            };

           

            $delegate.getKeys = function() {
                return keys;
            };

            return $delegate;
        }
    ]);`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.