Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular nsResource creates an array of char arrays for REST string arrays

Tags:

angularjs

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

like image 739
concept Avatar asked May 14 '13 21:05

concept


People also ask

How do you initialize a char array?

You can initialize a one-dimensional character array by specifying: A brace-enclosed comma-separated list of constants, each of which can be contained in a character. A string constant (braces surrounding the constant are optional)

How to initiate char array Java?

A char array can be initialized by conferring to it a default size. char [] JavaCharArray = new char [ 4 ]; This assigns to it an instance with size 4.

How do I add a character to a char array in Java?

append(char[] str) method appends the string representation of the char array argument to this sequence. The characters of the array argument are appended, in order, to the contents of this sequence. The length of this sequence increases by the length of the argument.

Can you store Strings in an array Java?

java String array works in the same manner. String Array is used to store a fixed number of Strings.


2 Answers

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;
  }

}

like image 140
user2849180 Avatar answered Sep 18 '22 15:09

user2849180


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

like image 25
tschiela Avatar answered Sep 19 '22 15:09

tschiela