资源保存对象数组

资源保存对象数组

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

问题描述

我使用 $资源来检索使用查询服务器上的数据。
服务器返回对象的数组,这是我在存储的 stuklijst 的。
我可以通过数组和循环使用$保存到阵列中的每个项目发回给服务器发送的 stuklijst 的的(更新)的内容返回到服务器。
我现在要发送的所有项目(整个的 stuklijst 的),以一气呵成服务器,而不使用循环。

在尝试了$上的 stuklijst 的保存,角抛出destination.push不是一个函数的错误。
这怎么能与 $资源来完成

这里的code:

服务:

  VAR stukModule = angular.module('stuklijstServices',['ngResource'])
stukModule.factory('Stuklijsten',函数($资源){
 返回$资源('休息/ stuklijsten /:stuklijstID',{});
});

控制器:

  //从服务器获取数据
  $ scope.stuklijst = Stuklijsten.query({stuklijstID:$ routeParams.stuklijstID});//参见下面由服务器返回的数据样本
//用户可以更新数据并要求使用保存saveStuklijst//向他们发送回服务器(使用saveStuklijst(Stuklijst))
  $ scope.saveStuklijst =功能(lijst){
    //由一个发送从stuklijst名可享:
    对于(VAR I = 0; I< lijst.length;我++)
        {//的console.log(ⅰ);
         // console.dir(lijst [I]);
          lijst [I] .RowID = I
          F =新Stuklijsten(lijst [I]);
          F $保存({stuklijstID:$ routeParams.stuklijstID})。
        };
    };

由服务器返回Stuklijst并存储数据:

  [{名:K FlxMT在DG满足直径025厘米,线型0的ProdID:DG025KFLXMT,的RowID:7,单位:STK,数量:1},{名称:SPR FL DG中遇到直径025厘米,线型:0的ProdID:DG025SPRFL,的RowID:8, 单位:STK,数量:1},{名称:T FlxFl DG中遇到直径025厘米,线型:0的ProdID:DG025TFLXFL,的RowID:9 单位:STK,数量:0},{名称:VER PL EX在DG满足直径025厘米,线型:0的ProdID:DG025VERPLEX,的RowID :10,单位:STK,数量:0},{名称:K FlxMT光伏满足直径008厘米,线型:0的ProdID:PV008KFLXMT,的RowID :11,单位:STK,量:0}]


解决方案

您可以通过重新定义你的资源的保存函数指定<$ C发送对象的数组$ C> IsArray的= TRUE 像这样:

  stukModule.factory('Stuklijsten',['$资源',函数($资源){
    返回$资源(
        休息/ stuklijsten /:stuklijstID',
        {},
        {
            保存: {
                方法:POST,
                IsArray的:真
            }
        }
    );
}]);

然后,在你的控制器,你可以组装清单,并保存在一个HTTP请求(不健谈API):

  $ scope.saveStuklijst =功能(lijst){
    变种some_list = [];
    对于(VAR I = 0; I&LT; lijst.length;我++){
        lijst [I] .RowID = I
        F =新Stuklijsten(lijst [I]);
        some_list.push({stuklijstID:$ routeParams.stuklijstID});
    };
    Stuklijsten.save(some_list);

如果你想仍然能够发布单个对象,你可以使用相同的概念来创建一个 saveBulk 函数preserve原保存的单个对象。

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使用$资源保存对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:54