angular / angular.js

AngularJS - HTML enhanced for web apps!
https://angularjs.org
MIT License
58.84k stars 27.51k forks source link

Can't load array of strings/numbers #4314

Closed Leonidas-from-XIV closed 9 years ago

Leonidas-from-XIV commented 10 years ago

Hi,

I've tried to access a resource via REST that looks like this: ["abc", "def"]. Unfortunately, this returns an object that looks like this: [{0: "a", 1: "b", 2: "c"}, {0: "d", 1: "e", 2: "f"}], in other words it iterates over the strings and converts them to objects. When trying the same with an array of [42, 23] I get as an result [{},{}].

This is due to https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L488 which creates a new Resource which can't deal with strings (or numbers): https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L416 because it uses copy which handles neither strings nor numbers.

Thanks to @Lukas-Stuehrk for debugging this.

petebacondarwin commented 10 years ago

Resource objects are "objects" not arrays. So you can't initialize a Resource with an array. Are you saying that your REST interface returns an array? Have you read the bit in the docs about isArray: http://docs.angularjs.org/api/ngResource.$resource

Leonidas-from-XIV commented 10 years ago

My REST server returns an array of strings, isArray is set to true. I am not completely sure which bit about isArray you mean. It doesn't say anywhere that the elements of the array that is returned from the REST server have to be objects themselves.

ozgunduz commented 10 years ago

I have the same problem that the list of numbers. Any solution to this will be helpful. Thank you!

caitp commented 10 years ago

Would any of you be interested in looking into this? I can take a look at it on monday maybe

pkozlowski-opensource commented 10 years ago

Actually, $resource expects objects in the response and not primitive types. The reason for this is that there needs to be a place to "attach" instance level methods ($save, $delete etc.). We obviously can't add those methods to say, primitive ints.

It seems to me that in cases like this we are not getting full benefit from $resource as we simply can't use instance methods. I'm not sure if $resource is the best fit in this situation.

@ozgunduz @Leonidas-from-XIV what are the exact methods you would like to use from resource, apart from get? Would you expect to also save individual numbers, for example? If the only thing you need from the $resource is to query a list of numbers maybe $http is the right answer here?

In any case I think that we should at least have a clear error message from $resource is we decide to not support this case.

lgalfaso commented 9 years ago

https://github.com/angular/angular.js/issues/4314#issuecomment-37344446 provided the right reason, and there is no activity for quite a number of month. Closing this but feel free to reopen in case there is some issue on ng-resources