11/**
22 * 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
44 * @author Martin Gontovnikas <[email protected] > 55 * @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 ) {
717
818var restangular = angular . module ( 'restangular' , [ ] ) ;
919
@@ -61,6 +71,15 @@ restangular.provider('Restangular', function() {
6171 return this ;
6272 } ;
6373
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+
6483 config . withHttpValues = function ( httpLocalConfig , obj ) {
6584 return _ . defaults ( obj , httpLocalConfig , config . defaultHttpFields ) ;
6685 } ;
@@ -188,12 +207,14 @@ restangular.provider('Restangular', function() {
188207 oneUrl : 'oneUrl' ,
189208 allUrl : 'allUrl' ,
190209 customPUT : 'customPUT' ,
210+ customPATCH : 'customPATCH' ,
191211 customPOST : 'customPOST' ,
192212 customDELETE : 'customDELETE' ,
193213 customGET : 'customGET' ,
194214 customGETLIST : 'customGETLIST' ,
195215 customOperation : 'customOperation' ,
196216 doPUT : 'doPUT' ,
217+ doPATCH : 'doPATCH' ,
197218 doPOST : 'doPOST' ,
198219 doDELETE : 'doDELETE' ,
199220 doGET : 'doGET' ,
@@ -424,6 +445,7 @@ restangular.provider('Restangular', function() {
424445 * Add element transformers for certain routes.
425446 */
426447 config . transformers = config . transformers || { } ;
448+ config . matchTransformers = config . matchTransformers || [ ] ;
427449 object . addElementTransformer = function ( type , secondArg , thirdArg ) {
428450 var isCollection = null ;
429451 var transformer = null ;
@@ -434,17 +456,24 @@ restangular.provider('Restangular', function() {
434456 isCollection = secondArg ;
435457 }
436458
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 ) {
443460 if ( _ . isNull ( isCollection ) || ( coll === isCollection ) ) {
444461 return transformer ( elem ) ;
445462 }
446463 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+ }
448477
449478 return object ;
450479 } ;
@@ -461,8 +490,19 @@ restangular.provider('Restangular', function() {
461490 if ( ! force && ! config . transformLocalElements && ! elem [ config . restangularFields . fromServer ] ) {
462491 return elem ;
463492 }
464- var typeTransformers = config . transformers [ route ] ;
493+
465494 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 ] ;
466506 if ( typeTransformers ) {
467507 _ . each ( typeTransformers , function ( transformer ) {
468508 changedElem = transformer ( isCollection , changedElem ) ;
@@ -560,7 +600,7 @@ restangular.provider('Restangular', function() {
560600
561601 var url = this . base ( current ) ;
562602
563- if ( what ) {
603+ if ( what || what === 0 ) {
564604 var add = '' ;
565605 if ( ! / \/ $ / . test ( url ) ) {
566606 add += '/' ;
@@ -641,7 +681,7 @@ restangular.provider('Restangular', function() {
641681 Path . prototype = new BaseCreator ( ) ;
642682
643683 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 ) ;
645685 parts [ 2 ] = parts [ 2 ] . replace ( / [ \\ \/ ] + / g, '/' ) ;
646686 return ( typeof parts [ 1 ] !== 'undefined' ) ? parts [ 1 ] + parts [ 2 ] : parts [ 2 ] ;
647687 } ;
@@ -927,20 +967,16 @@ restangular.provider('Restangular', function() {
927967
928968 function addCustomOperation ( elem ) {
929969 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 ;
931978 _ . 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 ) ;
944980 } ) ;
945981 } ) ;
946982 elem [ config . restangularFields . customGETLIST ] = _ . bind ( fetchFunction , elem ) ;
@@ -950,7 +986,7 @@ restangular.provider('Restangular', function() {
950986 function copyRestangularizedElement ( fromElement , toElement ) {
951987 var copiedElement = angular . copy ( fromElement , toElement ) ;
952988 return restangularizeElem ( copiedElement [ config . restangularFields . parentResource ] ,
953- copiedElement , copiedElement [ config . restangularFields . route ] , true ) ;
989+ copiedElement , copiedElement [ config . restangularFields . route ] , copiedElement [ config . restangularFields . fromServer ] ) ;
954990 }
955991
956992 function restangularizeElem ( parent , element , route , fromServer , collection , reqParams ) {
@@ -1003,10 +1039,12 @@ restangular.provider('Restangular', function() {
10031039 return config . transformElem ( localElem , true , route , service , true ) ;
10041040 }
10051041
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 ) ;
10081044 _ . each ( collection , function ( elem ) {
1009- restangularizeElem ( parent , elem , route , false ) ;
1045+ if ( elem ) {
1046+ restangularizeElem ( parent , elem , route , fromServer ) ;
1047+ }
10101048 } ) ;
10111049 return collection ;
10121050 }
@@ -1074,6 +1112,11 @@ restangular.provider('Restangular', function() {
10741112 if ( ! _ . isArray ( data ) ) {
10751113 throw new Error ( 'Response for getList SHOULD be an array and not an object or something else' ) ;
10761114 }
1115+
1116+ if ( true === config . plainByDefault ) {
1117+ return resolvePromise ( deferred , response , data , filledArray ) ;
1118+ }
1119+
10771120 var processedData = _ . map ( data , function ( elem ) {
10781121 if ( ! __this [ config . restangularFields . restangularCollection ] ) {
10791122 return restangularizeElem ( __this , elem , what , true , data ) ;
@@ -1164,8 +1207,15 @@ restangular.provider('Restangular', function() {
11641207 var resData = response . data ;
11651208 var fullParams = response . config . params ;
11661209 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 !== '' ) {
11681213 var data ;
1214+
1215+ if ( true === config . plainByDefault ) {
1216+ return resolvePromise ( deferred , response , elem , filledObject ) ;
1217+ }
1218+
11691219 if ( operation === 'post' && ! __this [ config . restangularFields . restangularCollection ] ) {
11701220 data = restangularizeElem (
11711221 __this [ config . restangularFields . parentResource ] ,
@@ -1310,6 +1360,8 @@ restangular.provider('Restangular', function() {
13101360 serv . one = _ . bind ( one , ( parent || service ) , parent , route ) ;
13111361 serv . post = _ . bind ( collection . post , collection ) ;
13121362 serv . getList = _ . bind ( collection . getList , collection ) ;
1363+ serv . withHttpConfig = _ . bind ( collection . withHttpConfig , collection ) ;
1364+ serv . get = _ . bind ( collection . get , collection ) ;
13131365
13141366 for ( var prop in collection ) {
13151367 if ( collection . hasOwnProperty ( prop ) && _ . isFunction ( collection [ prop ] ) && ! _ . includes ( knownCollectionMethods , prop ) ) {
@@ -1351,5 +1403,5 @@ restangular.provider('Restangular', function() {
13511403 return createServiceForConfiguration ( globalConfiguration ) ;
13521404 } ] ;
13531405} ) ;
1354-
1355- } ) ( ) ;
1406+ return restangular . name ;
1407+ } ) ) ;
0 commit comments