Reload resource? (Without $route.reload())

1,505 views
Skip to first unread message

Matthias Andrasch

unread,
Sep 5, 2011, 8:28:33 AM9/5/11
to ang...@googlegroups.com
Quick question:

Is there a nice way to reload a resource? I want to do this after a form on the same page was submitted to the server and created a new record. I implemented it with $route.reload() right now, but I hope there is a better solution. 

$updateView does not reload resources I guess (as well as $eval). Another way would be to push the newly received object after save() to the existing objects, but I don't want to rely on this client-side-operation.

Thanks in advance, 
Matthias

Kai Groner

unread,
Sep 5, 2011, 11:13:26 AM9/5/11
to ang...@googlegroups.com
The xhr cache is available via the data attribute of the $xhr.cache service.  The service is unique for the scope it was created in, so you probably want to create your handle to it when you define your $resource, like:

Ctlr.$inject = ['$xhr.cache', '$resource'];
function Ctlr($xhr_cache, $resource) {
    var resource = $resource('/animal/:name', ...);

    function uncache_animal(name) {
        delete $xhr_cache.data['/animal/'+name];
    }
}

A nicer helper could be made by borrowing the route generating code from Resource.js.  The resource rewrite is supposed to provide control over resource caching, so you wouldn't need to maintain this code in the long term.



Kai

--
You received this message because you are subscribed to the Google Groups "angular" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/vC_XUZz1XE4J.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Matthias Andrasch

unread,
Sep 5, 2011, 3:47:36 PM9/5/11
to ang...@googlegroups.com
Hey Kai, 

I don't really get the the $xhr.cache thing.


When I trigger the delete of one $xhr_cache.data-item, there is no reload of the resource. Should there be an automatic reload? Or do I misunderstand?
If I execute another Resource-action (this.move()) $xhr_cache.data is empty afterwards, even when I comment out the delete line.

Thanks in advance for further tips!

Vojta Jina

unread,
Sep 5, 2011, 6:01:44 PM9/5/11
to ang...@googlegroups.com
Currently there isn't "reload" method. You can define your own or just call Resource.get()...
We will definitely consider this when rewriting $resource service.

V.

Kai Groner

unread,
Sep 5, 2011, 8:18:15 PM9/5/11
to ang...@googlegroups.com
I think I answered a question you didn't ask.  Sorry about the confusion.

There isn't an automatic reload.  Re-executing your query is all there is at the moment, and I think your use of verifyCache would take care of any caching issues.

The reason your move() method didn't update the cache is just that POSTs aren't cached.


Kai

--
You received this message because you are subscribed to the Google Groups "angular" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/Idwdq-JM6IEJ.
Reply all
Reply to author
Forward
0 new messages