AngularJS transformResponse

送分小仙女□ 提交于 2019-11-30 18:01:20

transformResponse is executed on $http level.

When you customise $resource actions with custom config object, that object is actually passed on to the underlying $http service. So if you specify a transformResponse callback, it will be executed on $http level, and the results of your transformation will be passed back on to $resource.

$resource service will instantiate new object from your response data (which is already transformed by the transformResponse callback) and this new object will be an instance of the $resource itself.

So, your car object will be an instance of the Car, but only for a moment, until it's properties are copied into a new $resource object.

Here's a simplistic view of the process:

  1. $resource service initiates request
  2. $http service sends request and receives the response
  3. $http service transforms the response (response is now instance of Car)
  4. $resource service receives the transformed response (from $http)
  5. $resource service makes an instance of itself using transformed response properties (result is now instance of $resource)

Anyway, I don't recommend decorating or extending the $resource service, because it's simpler to write your own implementation using $http service.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!