Intercept HTTP calls to prevent AJAX request and respond with data from memory

610 views
Skip to first unread message

Francisc

unread,
Jul 10, 2014, 8:18:24 AM7/10/14
to ang...@googlegroups.com
Hello,

I would like to be able to intercept all AJAX calls made with $http service to prevent the any XHR calls and respond with data from an Angular service (for example).
So the data is in memory, not from a remote resource.

I want to be able to to this at runtime at the switch of a button, e.g. "Cut server communication ON / OFF".

As far as I can see, HTTP request interceptors can only modify the config object and return it, but cannot return actual data as response.

How can this be achieved?

Thank you.

Charly Poly

unread,
Jul 10, 2014, 10:56:01 AM7/10/14
to ang...@googlegroups.com
Hi Francisc,

I think that you need is $cacheFactory, if you look at the doc, you will see that $http use caching when enabled.

You can provide you own class of caching, but it has to provide the same methods than $cacheFactory (see the doc).

Then, once you cache system implemented, put in your module .config() : 


So every time that $http make a request, it will call your cache service.

You see ?

Regards,
Charly.

Francisc

unread,
Jul 10, 2014, 1:43:53 PM7/10/14
to ang...@googlegroups.com
That's pretty clever, thanks.
However, some of the calls specifically set cache:false (i.e. authorization calls) which would mean the cache factory wouldn't even be notified right?
I'd have to set it to true for those calls when the switch is filpped I guess.

Also, is this *the best* way of doing this?
There's no way to write a request interceptor that just responds with some data without making the call?

Thank you.

Charly Poly

unread,
Jul 10, 2014, 4:47:49 PM7/10/14
to ang...@googlegroups.com
Yep, if you have a $http object with cache set to false, your cache system will be ignored ;)

I don't know if it's the best solution ever, but it seems simple and in accordance with the principles of Angular (testability, modularity).

Interceptors are just here to change request or response but, to my knowledge, you cannot make a function that update both request and response in a row.
With a request interceptor, you can only change request config or prevent the request by returning a rejected promise.
A response interceptor will not be helpful in this case either..

I think that a custom cache system is the best solution in this case.

Regards,
Charly.

On Thursday, 10 July 2014 14:18:24 UTC+2, Francisc wrote:

Sander Elias

unread,
Jul 11, 2014, 1:39:57 AM7/11/14
to ang...@googlegroups.com
Hi Francisc,

Besides using the cache, you can use your own version of $http.
using a promise it's fairly easy to mimic the interface of $http. If you write your own version of it
you can pre, and post process in your function. Like a decorator.

Regards
Sander 

Francisc

unread,
Jul 11, 2014, 9:54:26 AM7/11/14
to ang...@googlegroups.com
Thank you, Charly.

Francisc

unread,
Jul 11, 2014, 9:57:41 AM7/11/14
to ang...@googlegroups.com
Hi Sander,

The app is already built and using $http service in may services.
Would it be possible to decorate and then `un-decorate` the $http service to achieve what I want?

Sander Elias

unread,
Jul 11, 2014, 10:35:51 AM7/11/14
to ang...@googlegroups.com
Hi Francisc,

I didn't try an decorator on $http myself, but it never hurts to try. It might be an easy way around!
(which is exactly what you are asking for, with an decorator you replace $http with something of your
own, which in turn can call the original $http)


Regards
Sander

Jagadesh Paladugula

unread,
Jul 11, 2014, 11:09:27 AM7/11/14
to ang...@googlegroups.com
Hi,

I have solved the same problem but with basic array object.

Hope it helps you.

Before firing the request, I check the array object with the key.[In my case key is params string with the url].
If the response is there, I serve the response from the array object, else I fire the request and store the response in the array in success callback for future reference.

Regards,
Jagadesh


--
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 angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Regards
Jagadesh

Francisc

unread,
Jul 13, 2014, 10:54:33 AM7/13/14
to ang...@googlegroups.com
Thanks for the replies guys.

Sander: Would a decorator help me not touch code in any of my services (they just make $http(config) calls and return the promise). I've not used decorators in Angular befre.

Jagadesh: That would work, but it would require extensive changes in my code. I want a solution that's as less intrusive as possible.

Sander Elias

unread,
Jul 14, 2014, 4:03:47 AM7/14/14
to ang...@googlegroups.com
Francisc,

That is indeed the idea behind a decorator!

Regards
Sander
Reply all
Reply to author
Forward
0 new messages