restangular获得完整的json响应

时间:2014-10-09 16:57:52

标签: javascript angularjs restangular

我是restangular的新手并尝试将其集成到我的应用中。这是我到目前为止所做的:

 MyResource.getList(params).then((dt) ->
      console.log dt
    )

如果我使用$http.get,那么我会获得完整的json对象

{
 "param" : "a",
 "param2" : "b",
 "data": [
  {
    "id": 6,
    "name": "v1",
    "address": "some long address"
  },
  {
    "id": 8,
    "name": "vadsf",
    "address": "some long address again"
  },
  {
    "id": 13,
    "name": "asdf",
    "address": "another long address"
  }
 ]
}

但是有了restangular,我会跟进:

[
 {
    "id": 6,
    "name": "v1",
    "address": "some long address"
 },
 {
    "id": 8,
    "name": "vadsf",
    "address": "some long address again"
 },
 {
    "id": 13,
    "name": "asdf",
    "address": "another long address"
 }
]

如何才能获得完整的json而不仅仅是数据数组?

1 个答案:

答案 0 :(得分:0)

我整理出来了。在我的app config

        RestangularProvider.addResponseInterceptor((data, operation) ->
            if operation is 'getList'
              extractedData = data.data
              if data.total then extractedData.param = data.param
              if data.total then extractedData.param2 = data.param2
              return extractedData
            else data
        )