在 AngularJs 中使用 $resource 来保存对象数组 [英] using $resource in AngularJs to save array of objects

查看:31
本文介绍了在 AngularJs 中使用 $resource 来保存对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 (updated) contents of stuklijst back to the server by looping through the array and using $save to send each item of the array back to the server. I now want to send all items (the entire stuklijst) to the server in one go, without using the loop.

When trying a $save on stuklijst, Angular throws a "destination.push is not a function" error. How can this be accomplished with $resource?

Here's the code:

Service:

var stukModule = angular.module('stuklijstServices', ['ngResource'])
stukModule.factory('Stuklijsten', function($resource){
 return $resource('rest/stuklijsten/:stuklijstID', {} );
});

Controller:

//Get the data from server      
  $scope.stuklijst = Stuklijsten.query({stuklijstID: $routeParams.stuklijstID});

//See below for sample of data returned by server
//Users can update the data and request a save using saveStuklijst

//Send them back to server (using saveStuklijst(Stuklijst))
  $scope.saveStuklijst = function(lijst) {
    //sending items from stuklijst one by one :
    for(var i = 0; i < lijst.length; i++) 
        {// console.log(i);
         // console.dir(lijst[i]);
          lijst[i].RowID = i
          f = new Stuklijsten(lijst[i]); 
          f.$save({stuklijstID: $routeParams.stuklijstID}); 
        } ;
    };

Data returned by server and stored in Stuklijst:

 [{"Name":"K FlxMT in DG met diameter 025 cm","LineType":0,"ProdID":"DG025KFLXMT","RowID":7,"Unit":"stk","Quantity":1},{"Name":"SPR Fl in DG met diameter 025 cm","LineType":0,"ProdID":"DG025SPRFL","RowID":8,"Unit":"stk","Quantity":1},{"Name":"T FlxFl in DG met diameter 025 cm","LineType":0,"ProdID":"DG025TFLXFL","RowID":9,"Unit":"stk","Quantity":0},{"Name":"VER PL EX in DG met diameter 025 cm","LineType":0,"ProdID":"DG025VERPLEX","RowID":10,"Unit":"stk","Quantity":0},{"Name":"K FlxMT in PV met diameter 008 cm","LineType":0,"ProdID":"PV008KFLXMT","RowID":11,"Unit":"stk","Quantity":0}] 

解决方案

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.

这篇关于在 AngularJs 中使用 $resource 来保存对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆