1
1
/**
2
2
* Restful Resources service for AngularJS apps
3
- * @version v1.5.2 - 2016-02-08 * @link https://github.com/mgonto/restangular
3
+ * @version v1.6.0 - 2016-12-25 * @link https://github.com/mgonto/restangular
4
4
* @author Martin Gontovnikas <martin@gon.to>
5
5
* @license MIT License, http://www.opensource.org/licenses/MIT
6
- */ ( function ( ) {
6
+ */ ( function ( root , factory ) {
7
+ // https://github.com/umdjs/umd/blob/master/templates/returnExports.js
8
+ if ( typeof define === 'function' && define . amd ) {
9
+ define ( [ 'lodash' , 'angular' ] , factory ) ;
10
+ } else if ( typeof module === 'object' && module . exports ) {
11
+ module . exports = factory ( require ( 'lodash' ) , require ( 'angular' ) ) ;
12
+ } else {
13
+ // No global export, Restangular will register itself as Angular.js module
14
+ factory ( root . _ , root . angular ) ;
15
+ }
16
+ } ( this , function ( _ , angular ) {
7
17
8
18
var restangular = angular . module ( 'restangular' , [ ] ) ;
9
19
@@ -61,6 +71,15 @@ restangular.provider('Restangular', function() {
61
71
return this ;
62
72
} ;
63
73
74
+ /**
75
+ * Always return plain data, no restangularized object
76
+ **/
77
+ config . plainByDefault = config . plainByDefault || false ;
78
+ object . setPlainByDefault = function ( value ) {
79
+ config . plainByDefault = value === true ? true : false ;
80
+ return this ;
81
+ } ;
82
+
64
83
config . withHttpValues = function ( httpLocalConfig , obj ) {
65
84
return _ . defaults ( obj , httpLocalConfig , config . defaultHttpFields ) ;
66
85
} ;
@@ -188,12 +207,14 @@ restangular.provider('Restangular', function() {
188
207
oneUrl : 'oneUrl' ,
189
208
allUrl : 'allUrl' ,
190
209
customPUT : 'customPUT' ,
210
+ customPATCH : 'customPATCH' ,
191
211
customPOST : 'customPOST' ,
192
212
customDELETE : 'customDELETE' ,
193
213
customGET : 'customGET' ,
194
214
customGETLIST : 'customGETLIST' ,
195
215
customOperation : 'customOperation' ,
196
216
doPUT : 'doPUT' ,
217
+ doPATCH : 'doPATCH' ,
197
218
doPOST : 'doPOST' ,
198
219
doDELETE : 'doDELETE' ,
199
220
doGET : 'doGET' ,
@@ -424,6 +445,7 @@ restangular.provider('Restangular', function() {
424
445
* Add element transformers for certain routes.
425
446
*/
426
447
config . transformers = config . transformers || { } ;
448
+ config . matchTransformers = config . matchTransformers || [ ] ;
427
449
object . addElementTransformer = function ( type , secondArg , thirdArg ) {
428
450
var isCollection = null ;
429
451
var transformer = null ;
@@ -434,17 +456,24 @@ restangular.provider('Restangular', function() {
434
456
isCollection = secondArg ;
435
457
}
436
458
437
- var typeTransformers = config . transformers [ type ] ;
438
- if ( ! typeTransformers ) {
439
- typeTransformers = config . transformers [ type ] = [ ] ;
440
- }
441
-
442
- typeTransformers . push ( function ( coll , elem ) {
459
+ var transformerFn = function ( coll , elem ) {
443
460
if ( _ . isNull ( isCollection ) || ( coll === isCollection ) ) {
444
461
return transformer ( elem ) ;
445
462
}
446
463
return elem ;
447
- } ) ;
464
+ } ;
465
+
466
+ if ( _ . isRegExp ( type ) ) {
467
+ config . matchTransformers . push ( {
468
+ regexp : type ,
469
+ transformer : transformerFn
470
+ } ) ;
471
+ } else {
472
+ if ( ! config . transformers [ type ] ) {
473
+ config . transformers [ type ] = [ ] ;
474
+ }
475
+ config . transformers [ type ] . push ( transformerFn ) ;
476
+ }
448
477
449
478
return object ;
450
479
} ;
@@ -461,8 +490,19 @@ restangular.provider('Restangular', function() {
461
490
if ( ! force && ! config . transformLocalElements && ! elem [ config . restangularFields . fromServer ] ) {
462
491
return elem ;
463
492
}
464
- var typeTransformers = config . transformers [ route ] ;
493
+
465
494
var changedElem = elem ;
495
+
496
+ var matchTransformers = config . matchTransformers ;
497
+ if ( matchTransformers ) {
498
+ _ . each ( matchTransformers , function ( transformer ) {
499
+ if ( route . match ( transformer . regexp ) ) {
500
+ changedElem = transformer . transformer ( isCollection , changedElem ) ;
501
+ }
502
+ } ) ;
503
+ }
504
+
505
+ var typeTransformers = config . transformers [ route ] ;
466
506
if ( typeTransformers ) {
467
507
_ . each ( typeTransformers , function ( transformer ) {
468
508
changedElem = transformer ( isCollection , changedElem ) ;
@@ -560,7 +600,7 @@ restangular.provider('Restangular', function() {
560
600
561
601
var url = this . base ( current ) ;
562
602
563
- if ( what ) {
603
+ if ( what || what === 0 ) {
564
604
var add = '' ;
565
605
if ( ! / \/ $ / . test ( url ) ) {
566
606
add += '/' ;
@@ -641,7 +681,7 @@ restangular.provider('Restangular', function() {
641
681
Path . prototype = new BaseCreator ( ) ;
642
682
643
683
Path . prototype . normalizeUrl = function ( url ) {
644
- var parts = / ( h t t p [ s ] ? : \/ \/ ) ? ( .* ) ? / . exec ( url ) ;
684
+ var parts = / ( (?: h t t p [ s ] ? : ) ? \/ \/ ) ? ( .* ) ? / . exec ( url ) ;
645
685
parts [ 2 ] = parts [ 2 ] . replace ( / [ \\ \/ ] + / g, '/' ) ;
646
686
return ( typeof parts [ 1 ] !== 'undefined' ) ? parts [ 1 ] + parts [ 2 ] : parts [ 2 ] ;
647
687
} ;
@@ -927,20 +967,16 @@ restangular.provider('Restangular', function() {
927
967
928
968
function addCustomOperation ( elem ) {
929
969
elem [ config . restangularFields . customOperation ] = _ . bind ( customFunction , elem ) ;
930
- _ . each ( [ 'put' , 'post' , 'get' , 'delete' ] , function ( oper ) {
970
+ var requestMethods = { get : customFunction , delete : customFunction } ;
971
+ _ . each ( [ 'put' , 'patch' , 'post' ] , function ( name ) {
972
+ requestMethods [ name ] = function ( operation , elem , path , params , headers ) {
973
+ return _ . bind ( customFunction , this ) ( operation , path , params , headers , elem ) ;
974
+ } ;
975
+ } ) ;
976
+ _ . each ( requestMethods , function ( requestFunc , name ) {
977
+ var callOperation = name === 'delete' ? 'remove' : name ;
931
978
_ . each ( [ 'do' , 'custom' ] , function ( alias ) {
932
- var callOperation = oper === 'delete' ? 'remove' : oper ;
933
- var name = alias + oper . toUpperCase ( ) ;
934
- var callFunction ;
935
-
936
- if ( callOperation !== 'put' && callOperation !== 'post' ) {
937
- callFunction = customFunction ;
938
- } else {
939
- callFunction = function ( operation , elem , path , params , headers ) {
940
- return _ . bind ( customFunction , this ) ( operation , path , params , headers , elem ) ;
941
- } ;
942
- }
943
- elem [ name ] = _ . bind ( callFunction , elem , callOperation ) ;
979
+ elem [ alias + name . toUpperCase ( ) ] = _ . bind ( requestFunc , elem , callOperation ) ;
944
980
} ) ;
945
981
} ) ;
946
982
elem [ config . restangularFields . customGETLIST ] = _ . bind ( fetchFunction , elem ) ;
@@ -950,7 +986,7 @@ restangular.provider('Restangular', function() {
950
986
function copyRestangularizedElement ( fromElement , toElement ) {
951
987
var copiedElement = angular . copy ( fromElement , toElement ) ;
952
988
return restangularizeElem ( copiedElement [ config . restangularFields . parentResource ] ,
953
- copiedElement , copiedElement [ config . restangularFields . route ] , true ) ;
989
+ copiedElement , copiedElement [ config . restangularFields . route ] , copiedElement [ config . restangularFields . fromServer ] ) ;
954
990
}
955
991
956
992
function restangularizeElem ( parent , element , route , fromServer , collection , reqParams ) {
@@ -1003,10 +1039,12 @@ restangular.provider('Restangular', function() {
1003
1039
return config . transformElem ( localElem , true , route , service , true ) ;
1004
1040
}
1005
1041
1006
- function restangularizeCollectionAndElements ( parent , element , route ) {
1007
- var collection = restangularizeCollection ( parent , element , route , false ) ;
1042
+ function restangularizeCollectionAndElements ( parent , element , route , fromServer ) {
1043
+ var collection = restangularizeCollection ( parent , element , route , fromServer ) ;
1008
1044
_ . each ( collection , function ( elem ) {
1009
- restangularizeElem ( parent , elem , route , false ) ;
1045
+ if ( elem ) {
1046
+ restangularizeElem ( parent , elem , route , fromServer ) ;
1047
+ }
1010
1048
} ) ;
1011
1049
return collection ;
1012
1050
}
@@ -1074,6 +1112,11 @@ restangular.provider('Restangular', function() {
1074
1112
if ( ! _ . isArray ( data ) ) {
1075
1113
throw new Error ( 'Response for getList SHOULD be an array and not an object or something else' ) ;
1076
1114
}
1115
+
1116
+ if ( true === config . plainByDefault ) {
1117
+ return resolvePromise ( deferred , response , data , filledArray ) ;
1118
+ }
1119
+
1077
1120
var processedData = _ . map ( data , function ( elem ) {
1078
1121
if ( ! __this [ config . restangularFields . restangularCollection ] ) {
1079
1122
return restangularizeElem ( __this , elem , what , true , data ) ;
@@ -1164,8 +1207,15 @@ restangular.provider('Restangular', function() {
1164
1207
var resData = response . data ;
1165
1208
var fullParams = response . config . params ;
1166
1209
var elem = parseResponse ( resData , operation , route , fetchUrl , response , deferred ) ;
1167
- if ( elem ) {
1210
+
1211
+ // accept 0 as response
1212
+ if ( elem !== null && elem !== undefined && elem !== '' ) {
1168
1213
var data ;
1214
+
1215
+ if ( true === config . plainByDefault ) {
1216
+ return resolvePromise ( deferred , response , elem , filledObject ) ;
1217
+ }
1218
+
1169
1219
if ( operation === 'post' && ! __this [ config . restangularFields . restangularCollection ] ) {
1170
1220
data = restangularizeElem (
1171
1221
__this [ config . restangularFields . parentResource ] ,
@@ -1310,6 +1360,8 @@ restangular.provider('Restangular', function() {
1310
1360
serv . one = _ . bind ( one , ( parent || service ) , parent , route ) ;
1311
1361
serv . post = _ . bind ( collection . post , collection ) ;
1312
1362
serv . getList = _ . bind ( collection . getList , collection ) ;
1363
+ serv . withHttpConfig = _ . bind ( collection . withHttpConfig , collection ) ;
1364
+ serv . get = _ . bind ( collection . get , collection ) ;
1313
1365
1314
1366
for ( var prop in collection ) {
1315
1367
if ( collection . hasOwnProperty ( prop ) && _ . isFunction ( collection [ prop ] ) && ! _ . includes ( knownCollectionMethods , prop ) ) {
@@ -1351,5 +1403,5 @@ restangular.provider('Restangular', function() {
1351
1403
return createServiceForConfiguration ( globalConfiguration ) ;
1352
1404
} ] ;
1353
1405
} ) ;
1354
-
1355
- } ) ( ) ;
1406
+ return restangular . name ;
1407
+ } ) ) ;
0 commit comments