I'm asking this because it makes me think I mustn't be doing something the 'angular' way, because my code is unnecessarily complicated.
I've defined a resource
Question = $resource("/questions/:id", {id: "@id"});
now if I do
Question.get(1);
It will go to /questions/1 and expect an object as a response If I do
Question.query({id: 1});
It will go to /questions/1 but expect an array as the response
I get why it does this because of isArray default setting.
I have client side code that I don't know how many id's it will be querying. I can easily work around this by having my angular code do
if (ids.length == 1)
Question.get(ids);
else
Question.query({id: ids})
and having my server send an object when array length is 1, but this seems overly complicated and makes me think I must be going about this the wrong way.
Is there a cleaner solution?
Not sure if this is the angular way, but the solution I came to which is slightly cleaner was to use different parameters on my query so that it hits the index method on rails instead of show and so can safely always return an array.
Like so:
Question = $resource("/questions/:id", {id: "@id"});
q = Question.get(1);
// Performs GET /questions/1
// q = { id: 1, ...};
q = Question.query({ids: 1});
// Performs GET /questions?ids=1
// q = [{id: 1, ... }];
q = Question.query({ids: [1,2]});
// Performs GET /questions?ids=1&ids=2
// q = [{id: 1, ... }, {id: 2, ...}];