using $resource in AngularJs to save array of objects

后端 未结 1 666
抹茶落季
抹茶落季 2021-01-05 18:07

I am using $resource to retrieve data from the server using query. The server returns an array of objects, which I store in stuklijst. I can send the (

相关标签:
1条回答
  • 2021-01-05 19:00

    You can send an array of objects by re-defining your resource's save function to specify isArray=true like so:

    stukModule.factory('Stuklijsten', ['$resource', function ($resource) {
        return $resource(
            'rest/stuklijsten/:stuklijstID',
            {},
            {
                save: {
                    method: 'POST',
                    isArray: true
                }
            }
        );
    }]);
    

    Then, in your controller, you can assemble the list and save all in one http request (less chatty API):

    $scope.saveStuklijst = function(lijst) {
        var some_list = [];
        for(var i = 0; i < lijst.length; i++) {
            lijst[i].RowID = i
            f = new Stuklijsten(lijst[i]); 
            some_list.push({stuklijstID: $routeParams.stuklijstID}); 
        };
        Stuklijsten.save(some_list);
    

    If you wanted to still be able to POST single objects, you could use the same concept to create a saveBulk function to preserve the original save for the single objects.

    0 讨论(0)
提交回复
热议问题