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

Add `where`, `isArray` and `clearChildren` options to () method (complete #8) #39

Closed jtomaszewski closed 10 years ago

jtomaszewski commented 10 years ago

This allows us to use the $clearCache() method in the following ways:

// clears all CommentsResource entries
CommentsResource.$clearCache()

// clears /?id=68 (2 methods)
CommentsResource.$clearCache({ where: [{ id: 68 }] })
CommentsResource.$clearCache({ where: { id: 68 } })

// clears only /array/?recent=true
CommentsResource.$clearCache({ where: { recent: true }, isArray: true, clearChildren: false })

// clears /array/?recent=true and all entries that belong to the array
CommentsResource.$clearCache({ where: { recent: true }, isArray: true, clearChildren: true })

// clears all CommentsResource entries except for /?id=68 (2 methods)
CommentsResource.$clearCache({ exceptFor: [{ id: 68 }] })
CommentsResource.$clearCache({ exceptFor: { id: 68 } })

// clears all CommentsResource entries except for /array/?recent=true and all entries that belong in which array
CommentsResource.$clearCache({ exceptFor: { recent: true }, isArray: true, clearChildren: false })

// clears all CommentsResource entries except for /array/?recent=true (but still clears all entries which belong in that array)
CommentsResource.$clearCache({ exceptFor: { recent: true }, isArray: true, clearChildren: true })

Tests for where added, tests for exceptFor are still working =)

NOTE This will break existing implementations, because:

BEFORE

// clears all CommentsResource entries except for /?id=68
CommentsResource.$clearCache({ exceptFor: [{ id: 68 }] })

// clears all CommentsResource entries except for /array/?recent=true and all entries that belong to that array
CommentsResource.$clearCache({ exceptFor: { id: 68 } })

AFTER

// clears all CommentsResource entries except for /?id=68 (2 methods)
CommentsResource.$clearCache({ exceptFor: [{ id: 68 }] })
CommentsResource.$clearCache({ exceptFor: { id: 68 } })

// clears all CommentsResource entries except for /array/?recent=true and all entries that belong to that array
CommentsResource.$clearCache({ exceptFor: { recent: true }, isArray: true })

However, we could add a polyfill for old $clearAll() method which would make it working for older implementations also. with a deprecation log message too, ofcourse

hazeledmands commented 10 years ago

Wow, thanks so much for this :)

hazeledmands commented 10 years ago

Merged manually. Nice job!