How to Restangularize items in collection off parent object

Issue

I am returning an object using Restangular over AngularJS (GPT object is the parent object being returned), with an array being returned corresponding to the projects withing the GPT.

I can do all the Restangular “stuff” like save() etc. on the parent GPT object. However, when I get a reference to individual items in the “projects” collection, I am unable to do a Restangular save() on it. How do I make sure all items returned in the collections below the main object are “Restangularised” so I can perform restful operations on them? i.e. I guess I want a “deep Restangularisation” if that makes sense ;-)…if not, how do I Restangularize an instance just before I attempt to do a save() operation and provide the relevant URL for the PUT/POST etc.

Hope this makes sense.

Regards

i

Solution

There is a Restangular.restangularizeElement method.

You can use it as follows (for collection):

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

source

Answered By – smackenzie

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.