Skip to content

Commit 94ffaf0

Browse files
committed
Add configuration option to use plain() by default
1 parent f934396 commit 94ffaf0

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ You can also **check out [a video introduction of a talk I gave at Devoxx France
4949
- [setDefaultHeaders](#setdefaultheaders)
5050
- [setRequestSuffix](#setrequestsuffix)
5151
- [setUseCannonicalId](#setusecannonicalid)
52+
- [setPlainByDefault](#setplainbydefault)
5253
- [How to configure them globally](#how-to-configure-them-globally)
5354
- [Configuring in the config](#configuring-in-the-config)
5455
- [Configuring in the run](#configuring-in-the-run)
@@ -552,6 +553,10 @@ If all of your requests require to send some suffix to work, you can set it here
552553

553554
You can set this to either `true` or `false`. By default it's false. If set to true, then the cannonical ID from the element will be used for URL creation (in DELETE, PUT, POST, etc.). What this means is that if you change the ID of the element and then you do a put, if you set this to true, it'll use the "old" ID which was received from the server. If set to false, it'll use the new ID assigned to the element.
554555

556+
#### setPlainByDefault
557+
558+
You can set this to `true` or `false`. By default it's false. If set to true, data retrieved will be returned with no embed methods from restangular.
559+
555560
#### setEncodeIds
556561

557562
You can set here if you want to URL Encode IDs or not. By default, it's true.

src/restangular.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ restangular.provider('Restangular', function() {
5555
config.defaultHttpFields = values;
5656
return this;
5757
};
58+
59+
/**
60+
* Always return plain data, no restangularized object
61+
**/
62+
config.plainByDefault = config.plainByDefault || false;
63+
object.setPlainByDefault = function(value) {
64+
config.plainByDefault = value === true ? true : false;
65+
return this;
66+
}
5867

5968
config.withHttpValues = function(httpLocalConfig, obj) {
6069
return _.defaults(obj, httpLocalConfig, config.defaultHttpFields);
@@ -1071,6 +1080,11 @@ restangular.provider('Restangular', function() {
10711080
if (!_.isArray(data)) {
10721081
throw new Error('Response for getList SHOULD be an array and not an object or something else');
10731082
}
1083+
1084+
if (true === config.plainByDefault) {
1085+
return resolvePromise(deferred, response, data, filledArray);
1086+
}
1087+
10741088
var processedData = _.map(data, function(elem) {
10751089
if (!__this[config.restangularFields.restangularCollection]) {
10761090
return restangularizeElem(__this, elem, what, true, data);
@@ -1161,8 +1175,14 @@ restangular.provider('Restangular', function() {
11611175
var resData = response.data;
11621176
var fullParams = response.config.params;
11631177
var elem = parseResponse(resData, operation, route, fetchUrl, response, deferred);
1178+
11641179
if (elem) {
11651180
var data;
1181+
1182+
if (true === config.plainByDefault) {
1183+
return resolvePromise(deferred, response, elem, filledObject);
1184+
}
1185+
11661186
if (operation === 'post' && !__this[config.restangularFields.restangularCollection]) {
11671187
data = restangularizeElem(
11681188
__this[config.restangularFields.parentResource],

test/restangularSpec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,4 +1143,32 @@ describe("Restangular", function() {
11431143
$httpBackend.flush();
11441144
});
11451145
});
1146+
1147+
describe("setPlainByDefault", function() {
1148+
1149+
it("should not add restangularized methods to response object", function() {
1150+
var newRes = Restangular.withConfig(function(RestangularConfigurer) {
1151+
RestangularConfigurer.setPlainByDefault(true);
1152+
});
1153+
1154+
expect(newRes.configuration.plainByDefault).toEqual(true);
1155+
1156+
newRes.one('accounts',0).get().then(function(account) {
1157+
expect(account).toEqual(accountsModel[0]);
1158+
});
1159+
1160+
$httpBackend.flush();
1161+
});
1162+
1163+
it("shoud not add restangularized methods to response collection", function() {
1164+
var newRes = Restangular.withConfig(function(RestangularConfigurer) {
1165+
RestangularConfigurer.setPlainByDefault(true);
1166+
});
1167+
1168+
newRes.all('accounts').getList().then(function(accounts){
1169+
expect(accounts).toEqual(accountsModel);
1170+
});
1171+
$httpBackend.flush();
1172+
});
1173+
});
11461174
});

0 commit comments

Comments
 (0)