goodeggs / angular-cached-resource

An AngularJS module to interact with RESTful resources, even when browser is offline
MIT License
216 stars 29 forks source link

How to use $clearCache #53

Closed sayacoub closed 9 years ago

sayacoub commented 9 years ago

Hi everyone!

I've started using angular cached resource and it works very well except for the clearing, that doesn't seem to work for me. I've tried very simples things, but none worked.

var test = $cachedResource('Example', 'myURL',{Id: 'unid'}, { query: { method: 'GET', isArray:true, } }); var kel= test.query({count: 3});

When I run this code I get this in my local storage: cachedResource://Example?Id=C1257902002AE240C1257B2B005C4B48 cachedResource://Example?Id=C1257902002AE240C1257B2B005C4B4A cachedResource://Example?Id=C1257902002AE240C1257B2B005C4B4B

And if I try to add this, nothing happens:

kel.$httpPromise.then(function(data) { test.$clearCache({where: {Id: "C1257902002AE240C1257B2B005C4B48"}}); });

I've also tried with "exceptFor", but I get the same result.

Am I missing something? Thanks in advance, Sami

hazeledmands commented 9 years ago

Glad you're getting some use out of this module! When clearing the cache, try this instead:

test.$clearCache({where: {unid: "C1257902002AE240C1257B2B005C4B48"}});

This would also work:

kel.$httpPromise.then(function(data) {
  test.$clearCache({where: data[0]})
})

$clearCache expects to get representations of the cached objects themselves, as opposed to pointers to those objects, if that makes sense.

sayacoub commented 9 years ago

Thanks for the quick answer! Your solution worked perfectly, I should have thought of it before!

But it also created another problem that I can't seem to fix: I used your second solution like this:

kel.$httpPromise.then(function(data) {
  test.$clearCache({exceptFor : data})
})

It clears all elements that are not available anymore, but it also clears this :

cachedResource://Example/array?category=John_Doe&count=3

when I use this:

var kel= test.query({count: 3, category: "John_Doe"});

So the problem is that without this resource, I can't access the elements when I'm offline. What syntax should i use to add the array resource to the exceptFor array?

And once again, thanks for your help and your awesome work on this module!

Sami

hazeledmands commented 9 years ago

Hey Sami,

If that's what you'd like to accomplish, try this:

kel.$httpPromise.then(function() {
  test.$clearCache({
    exceptFor: {count: 3, category: "John_Doe"},
    isArray: true,
    clearChildren: false
  });
})

Hope that's helpful.

sayacoub commented 9 years ago

Exactly what I needed! Thanks again!