9

I have a web service controller that serves up "activity" data

GET /api/activity/list
GET /api/activity/1
GET /api/activity/activity-slug-name
PUT /api/activity
DELETE /api/activity/1

It also serves up some "meta" data

GET /api/activity/meta/dates
GET /api/activity/meta/states

They all work extremely well using ngResource as they all return JSON objects. However the /api/activity/meta/dates does not

It returns an array of strings

[
    "2013-06-02T17:05:16Z",
    "2013-06-07T17:05:16Z",
    "2013-08-17T17:05:16Z"
]

ngResource turns this into an array of char arrays see: Invalid result from ngResource request with string array

Obviously i could just split the meta functions out into some form of ActivityMetaService but i would prefer to keep it all together

My question is

Is there a way to stop ngResource from performing this overzealous object decomposition?

Or should i rais a bug with angular?

Thanks in advance

* UPDATE *

Thanks Mark for your comment. If you had placed it as an answer I would have accepted it, because it points to a bug in Angular and the other two answers do not fit with the current architecture.

While you are technical correct, no one has though of Strings as an array of chars since the mid 80s, C/C++ programmers aside :)

I have raised a ticket on github with the angular.js project, reference below

https://github.com/angular/angular.js/issues/2664

Community
  • 1
  • 1
concept
  • 761
  • 2
  • 10
  • 16
  • 2
    This happens because of the use of `angular.copy()` inside of `$resource()`. When the `copy()` function finds an array of strings it treats the string as an array of characters (which it actually is). There is no way that I know of to disable this behavior the only real way now is to use `$http` service directly. – Mark Coleman May 14 '13 at 22:43

3 Answers3

2

Our you could try to use the transformRequest

query: {
  method: 'GET',
  isArray: true,
  transformResponse: function (data, headers) {
    var tranformed = [];
    [].forEach.call(eval(data), function (d) {
        tranformed.push({ name: d });
    });
    return tranformed;
  }

}

1

To get only an array of strings use $http service instead.

tschiela
  • 5,231
  • 4
  • 28
  • 35
1

Yep!

https://github.com/klederson/ModelCore uses $http service from the scratch to be a little bit easier check it out!

Klederson Bueno
  • 330
  • 3
  • 6