mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

Response to post is not being restangularized #669

Open mark0978 opened 10 years ago

mark0978 commented 10 years ago

I use getList and my list of objects returned are all restangularized so that I can do things like .put.

When I use post(obj) to create a new object, the returned object is not restangularized, rather it is just a regular object. The objects do appear to be going thru my processResponse (strings are converted to dates as expected), but all of the restangular methods are missing.

This particular post sends an array of elements (to create multiple records in one request). The response is an array of objects, just like getList(). Restangular doesn't pick up on that return type and decorates the array object with the restangular methods instead of the element or elements within it.

What should be done here? Can you not post multiple records for creation?

mark0978 commented 10 years ago

Ok, I have some code that will fix this, but I seriously doubt this is the way to do this. I find restangular very hard to follow with all the .bind stuff that goes on in the code. Seems more complicated that it needs to be, but maybe I'm just not understanding all you are doing.

Here is the patch I'm using.

                  var okCallback = function(response) {
                      var resData = response.data;
                      var fullParams = response.config.params;
                      var data = parseResponse(resData, operation, route, fetchUrl, response, deferred);
                      if (data) {

                        if (operation === "post" && !__this[config.restangularFields.restangularCollection]) {
                          resolvePromise(deferred, response, restangularizeElem(__this, data, what, fullParams), filledObject);
                        } else {
                            if(_.isArray(data)) {
                                var processedData = _.map(data, function(elem) {
                                    if (!__this[config.restangularFields.restangularCollection]) {
                                        return restangularizeElem(__this, elem, what, data);
                                    } else {
                                        return restangularizeElem(__this[config.restangularFields.parentResource], elem, __this[config.restangularFields.route], data);
                                    }
                                });
                                processedData = _.extend(data, processedData);
                                resolvePromise(deferred, response, restangularizeCollection(__this[config.restangularFields.parentResouce], data, __this[config.restangularFields.route], fullParams), filledObject);
                            } else {
                                resolvePromise(deferred, response, restangularizeElem(__this[config.restangularFields.parentResource], data, __this[config.restangularFields.route], fullParams), filledObject);
                            }
                        }

                      } else {
                        resolvePromise(deferred, response, undefined, filledObject);
                      }
                  };
mgonto commented 10 years ago

What should be done here? Can you not post multiple records for creation?

You can post multiple records for creation but Restangular can't handle the multiple elements response for now.

Without that patch, what you can do is actually is call restangularizeCollection but I'll try to get it fixed.

Marking as enhancement :).

Thanks!