mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

Wrapping Restangular to add features #373

Closed ghost closed 11 years ago

ghost commented 11 years ago

Restangular handles API consumption as I want, but I need to add some features. I assume that I'll need to create wrapper methods around the Restangular API, but I'd like to get your input on how best to achieve this.

  1. Retrieved data should be cached, and then subsequent requests should resolve immediately, but a HTTP request is still sent and updates the objects silently when a response is received
  2. Live changes to data should show in all browsers immediately. We have a node instance which sends updates to data via socket.io, so I'll need to add listeners for these messages and update the objects/cache as necessary.
mgonto commented 11 years ago

Hey,

So I'd use a $cache for this. Do your own implementation for a cache instead of getting one from $cacheFactory and then you could use that.

Basically, Restangular use $http which would always return the stuff in the cache. But, you could (in your cache), every time that someone wants to get something from cache, do the request and once it's resolved, update that cache value, then, next time it's called it'll be updated.

Check http://docs.angularjs.org/api/ng.$cacheFactory to see how $cache interface works.

Hope this works for you!

If you do implement this and you want, you can PR me later and I can add this as a separate JS file as a Restangular plugin :) .

Thanks!!

If you have any other doubt or problem, please post it here!