Batch update, create and delete the REST way
Asked Answered
D

1

6

Using Tastypie and AngularJS $resource I would like to perform a set of update, create and delete operations.

Currently, I broadcast an event:

$rootScope.$broadcast('save');

That event is captured by each controller responsible for creating, updating and deleting using the $resource service:

ResourceService.update({id:$scope.id}, $scope.element).$promise.then(function(element) {
    $scope.$emit('saved');
});

Now, this cause some race conditions both at client side and on server side.

What would be the simplest way to perform this set of operations as a batch in the REST way?

Distribution answered 29/9, 2014 at 14:21 Comment(2)
What do you mean there is a race condition? If you have more than one place that actually performs an XHR upon the 'save' event, of course you can't control which of them arrive first to your server. If you have a certain order you want things to be done with, you need to run an xhr, wait for it to come back, and then run the second - or create a specific endpoint in the server where you send all the data and the server takes care of the orderPadauk
Thanks, @Barnash. Sending each xhr one by one would be too slow. My problem is that $resource does not support sending all the data in a single xhr. Do I have to implement the 'batch' xhr using $http or is there an api such as Restangular that will do it for me?Distribution
M
0

I recently played around with Angular HTTP Batcher

however if you want a more generic JS only async helper you can use Async

I think those are mostly what you are looking for, the blog post from the Angular HTTP Batcher is a good read.

Muttonchops answered 1/10, 2014 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.