How can I disable $resource cache for good?

2,058 views
Skip to first unread message

Witold Szczerba

unread,
Jan 8, 2012, 4:31:59 PM1/8/12
to ang...@googlegroups.com
Hi,
I always preferred using $xhr over $resource, as the latter does not
give you much options when it comes to fine-grained control over what
is happening. Unfortunately, $xhr (and new $http) does not implement
mechanism for creating request parameters like $resource does.
This is why I wanted to give $resource a second chance. I like the way
it maps query criteria like this:

function IssueListController($resource) {
var scope = this;
var Issue = $resource('rest/issues/list');
scope.issues = [];
scope.criteria = {};

scope.getIssues = function() {
scope.issues = Issue.query(scope.criteria);
}
}

"scope.criteria" is bound to the form, so user can fill them and
$resource will generate URL like:
/issues/list?receiptDate=2012-01-01&signature=ABC

I wish I could use plain-old $xhr/$http, because I am starting to
remember why I never liked $resource. By default it uses $xhr.cache,
so once user asks for issues, the result will never be updated. New
issues can be registered by a bunch of users, but $resource will keep
saying there is nothing new on the server (oh why... why?).

I know it is possible to disable cache by overriding or registering
new methods on types generated by $resource, but maybe there is some
nice, clean and simple way to get rid of $resource caching "feature"
globally?

Thanks, Angular rulez! :)
Witold Szczerba

Daniel Nelson

unread,
Jan 8, 2012, 4:37:46 PM1/8/12
to ang...@googlegroups.com
If you set $xhr defaults, they carry through to $resource (which is built on $xhr). For example:

  $xhr.defaults.headers.post['Content-Type'] = 'application/json'
  $xhr.defaults.headers.put['Content-Type'] = 'application/json'

  // assumes the presence of jQuery
  var token = $("meta[name='csrf-token']").attr("content");
  $xhr.defaults.headers.post['X-CSRF-Token'] = token;
  $xhr.defaults.headers.put['X-CSRF-Token'] = token;
  $xhr.defaults.headers['delete']['X-CSRF-Token'] = token;

I'm not sure what the default is for caching, but I would think there would be an option.

Vojta Jina

unread,
Jan 8, 2012, 6:48:20 PM1/8/12
to ang...@googlegroups.com
Hey Witold,

in 0.10.6 caching is disabled. We only cache templates (ng:view, ng:include)...

V.

Daniel

unread,
Jan 28, 2012, 9:02:47 AM1/28/12
to AngularJS
> in 0.10.6 caching is disabled. We only cache templates (ng:view,
> ng:include)...

Hello Vojta,

I find myself in need of enabling caching on a $resource. I see that
$http can be called with a "cache: true" option, but that won't work
for a resource, and $httpProvider.defaults.cache is not available.

My use case is a text field that displays a message in response to
what the user has typed. It is set up to only ping the server when the
user pauses, but it would be better if it only hit the server once for
any given string the user typed. I would rather use $resource than
straight up $http because the data being returned include more than
just the message, so $resource is perfect, apart from the caching.

Thank You,

Daniel

Vojta Jina

unread,
Jan 28, 2012, 4:47:07 PM1/28/12
to ang...@googlegroups.com
Hey Daniel,

you are right, you can pass cache: true or even cache object (passing just true actually uses the default instance of cache, provided by $cacheFactory) with $http.

In your case, I think using $http is fine, would you miss any $resource features ?

It should be pretty strightforward to implement this into $resource (basically adding a config property to action definition - cache), so pull request welcomed.

V.
Reply all
Reply to author
Forward
0 new messages