当集合服务返回字典而不是数组时,如何使用Restangular?

Dan*_*ita 2 rest dictionary angularjs

我正处于开发客户端应用程序到现有REST服务的早期阶段,我正在尝试决定使用什么来进行服务器通信.到目前为止,我喜欢Restangular文档,它看起来非常可靠,但我担心它不能与服务一起使用,因为响应看起来像这样:

{
  "0": {
    "name": "John",
    "total": 230,
    "score": 13
  },
  "1": {
    "name": "Sally",
    "total": 190,
    "score": 12
  },
  "2": {
    "name": "Harry",
    "total": 3,
    "score": 0
  },
  "...": "..."
}
Run Code Online (Sandbox Code Playgroud)

我在文档中找不到支持这样的东西或者我应该如何处理这种类型的响应.有人试过吗?有任何想法吗?

mgo*_*nto 7

我是Restangular :)的创造者.

您可以将该响应与Restangular一起使用.您需要使用responseInterceptor.我想当你得到一个阵列时,你的回答看起来就像那样.所以你需要这样做:

RestangularProvider.setListTypeIsArray(false)
RestangularProvider.setResponseExtractor(function(response, operation) {
  // Only for lists
  if (operation === 'getList') {
    // Gets all the values of the property and created an array from them
    return _.values(response)
  }
  return response;
});
Run Code Online (Sandbox Code Playgroud)

有了它,它会工作:)

请告诉我你是否能解决这个问题