Ended up wrapping it in a module (needs more work): https://gist.github.com/elado/a6f5f1f8edade63a3b9d
And reported as a feature request: https://github.com/angular/angular.js/issues/8739 On Friday, August 22, 2014 4:34:37 PM UTC-7, Elad Ossadon wrote: > > I have a simple resource of categories, with a cached query action: > > > app.factory 'Category', ($resource) -> > $resource "/categories/:id", {id: '@id'}, { > query: { cache: true, isArray: true, method: 'GET' } > } > > I have multiple controllers/directives that call Category.query() to get > all categories. The request is cached so it doesn't execute multiple HTTP > requests, which is fine, but turns out that angular-resource returns > different Category instances every time. If I edit one category on a > directive it doesn't update the rest, simply because it's not the same > instance. > > *My Question: What's the recommended way to share all the instances of a > query between all callers when using angular-resource?* > > > My current solution is ugly: I override the query method, call the request > only once, cache the result in a variable, and preserve all the callbacks > in an array, then call them at once when the request is done. It's ugly but > it works. > > _all = null > _loading = false > _queryPromises = [] > oldQuery = Category.query > > Category.query = -> > deferred = $q.defer() > > if _all > deferred.resolve(_all) > else > _queryPromises.push deferred > > if !_loading > _loading = true > oldQuery (all) -> > _loading = false > _all = all > > _queryPromises.forEach (promise) -> > promise.resolve(_all) > _queryPromises.length = 0 > > deferred.promise > > It currently returns a promise and doesn't use a callback like > angular-resource but this is an easy change. > > I tried angular-cached-resource but got the same result. > > -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
