3

我正在为 AppEngine 中的 RESTFul 后端做 AppEngine 端点。我在客户端使用 AngujarJS,使用 ngResource 管理服务器数据。

我的问题:我无法从 AppEngine Endpoint 返回纯数组。我试过这个:

@ApiMethod(
        name = "mpscorerapi.getAllResults",
        path = "/tournament/{tournamentId}/result/"
        httpMethod = HttpMethod.GET
)
public List<SimpleResult> getAllResults(@Named("tournamentId") Long tournamentId) throws NotFoundException
{
   ...
}

虽然这将数据从服务器传递到客户端,但它并没有构建“SimpleResult”对象的数组,而是一个包含 SimpleResult 数组的单个对象,称为“items”:

  {
 "items": [
  {
   "id": "5733953138851840",
   "h": 0,
   "r": 0,
   "kind": "mpscorer#mpscorerapiItem"
  },
  {
   "id": "5733953138851841",
   "h": 1,
   "r": 2,
   "kind": "mpscorer#mpscorerapiItem"
  }
 ],
 "kind": "mpscorer#mpscorerapi",
 "etag": "\"SALE0WnK41Jo38zV0ILO62-rVOI/Mh2G6GGztZv-wj_56Kjf1o1XBaM\""
}

这使得 ngResource 非常无用,因为“查询”方法需要一个纯数组作为回复:

$scope.resultsSrv = Result.query({tournamentID:tournamentId}) //fails!!!!

关于如何获得“SimpleResult”数组的任何想法?

谢谢!

4

2 回答 2

1

您必须转换请求,例如如下:

angular.module('myApp')
  .factory('Result', function Result($http, $resource) {
    var Result = $resource('/tournament/:tournamentId/result/', {}, {
      query: {
        method: 'GET', 
        isArray: true,
        transformResponse: [].concat($http.defaults.transformResponse, function transformResponse(data) {
          return data.items;
        })
      }
    });
    return Result;
  });
于 2015-08-26T08:09:18.217 回答
0

我也有类似的挑战。我最终使用 jQuery 来解析它。使用地图非常简单。

我的情况是:

$.map(locs.items, function (loc) {
                      return {
                          value: loc.longName
                      };
于 2014-12-31T13:51:00.317 回答