restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Restangularize subresources

Open gabzim opened this issue 11 years ago • 6 comments

Is there a simple way to tell restangular that a certain field is a collection of resources?

For instance in my app I can do:

GET to: /api/courses/123
Restangular.one('courses', 123).get();

/* OR */

GET to: /api/courses/123/students
Restangular.one('courses', 123).all('students').getList();

The first call however, fetches the course with a field students that already contains an array of the students in the course. Is there any way to say: "Restangular, this array is not merely an array of objects, but rather it's a student collection" so that then I can do things like students[0].remove()

Unrelated: Vamos a ganar el mundial o no?

gabzim avatar Jun 10 '14 20:06 gabzim

this would be cool!

matteosister avatar Jun 20 '14 09:06 matteosister

What about:

Restangular.one('courses', 123).get().then(function (course) {
  course.students = Restangular.restangularizeCollection(course, course.students, 'students');
  // You should now be able to do 'course.students[0].remove()'
  // And if you want to chain promises:
  return course;
});

pauldijou avatar Jul 01 '14 00:07 pauldijou

Hi @pauldijou ,

I'm looking for solution for this issue as well. Yours works for most cases, not all. Two major problem for Restangular.restangularizeCollection

  • It always set the fromServer flag to false. So it breaks the .save() method.
  • Per #727, it does not define the parent collection correctly.

gregwym avatar Nov 19 '14 06:11 gregwym

Any news about that ? Or any way in a tutorial to do this ?

mpoiriert avatar Jan 11 '16 01:01 mpoiriert

+1

akaNightmare avatar Aug 29 '16 12:08 akaNightmare

Can a more concrete example be provided for this?

daviesgeek avatar Aug 31 '16 02:08 daviesgeek