Skip to content

Commit

Permalink
Use response extractor to avoid overriding fields
Browse files Browse the repository at this point in the history
Fixes #273
  • Loading branch information
fzaninotto committed Feb 3, 2015
1 parent fcb7666 commit 468cd88
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/javascripts/ng-admin/Crud/CrudModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ define(function (require) {
return require('nprogress');
});

CrudModule.config(['RestangularProvider', function (RestangularProvider) {
// get response headers
RestangularProvider.setFullResponse(true);
// avoid Restangular to erase data by "restangularizing" elements
// @link https://github.com/mgonto/restangular/issues/100
RestangularProvider.setResponseExtractor(function(response) {
var newResponse = response;
if (angular.isArray(response)) {
newResponse.originalData = [];
angular.forEach(newResponse, function(value, key) {
newResponse.originalData[key] = angular.copy(value);
});
} else {
newResponse.originalData = angular.copy(response);
}

return newResponse;
});
}]);

/**
* Date Picker patch
* https://github.com/angular-ui/bootstrap/commit/42cc3f269bae020ba17b4dcceb4e5afaf671d49b
Expand Down
2 changes: 0 additions & 2 deletions src/javascripts/ng-admin/Crud/repository/Queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ define(function () {
this.$q = $q;
this.Restangular = Restangular;
this.config = Configuration();

this.Restangular.setFullResponse(true); // To get also the headers
}

Queries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration'];
Expand Down
8 changes: 6 additions & 2 deletions src/javascripts/ng-admin/Crud/repository/RetrieveQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ define(function (require) {
.oneUrl(view.entity.name(), this.config.getRouteFor(view, entityId))
.get()
.then(function (response) {
return view.mapEntry(response.data);
return view.mapEntry(response.data.originalData);
});
};

Expand Down Expand Up @@ -115,7 +115,11 @@ define(function (require) {
// Get grid data
return this.Restangular
.allUrl(listView.entity.name(), this.config.getRouteFor(listView))
.getList(params);
.getList(params)
.then(function(response) {
response.data = response.data.originalData;
return response;
});
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/test/mock/Restangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ define('mock/Restangular', [
getList: function() { return mixins.buildPromise({}); },
customPOST: function() { return mixins.buildPromise({}); },
customPUT: function() { return mixins.buildPromise({}); },
customDELETE: function() { return mixins.buildPromise({}); },
customDELETE: function() { return mixins.buildPromise({}); }
};

return Restangular;
Expand Down

0 comments on commit 468cd88

Please sign in to comment.