Skip to content

[WIP] Strip restangular methods from entries #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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