11/**
22 * API Bound Models for AngularJS
3- * @version v1.1.4 - 2014-11-26
3+ * @version v1.1.5 - 2014-12-10
44 * @link https://github.com/angular-platanus/restmod
55 * @author Ignacio Baixas <[email protected] > 66 * @license MIT License, http://www.opensource.org/licenses/MIT
@@ -373,7 +373,7 @@ RMModule.provider('restmod', [function() {
373373
374374 if ( arguments . length > 1 ) {
375375 model . mix ( arraySlice . call ( arguments , 1 ) ) ;
376- $log . warn ( 'Passing mixins and difinitions in the model method will be deprecated in restmod 1.2, use restmod.model().$ mix() instead.' ) ;
376+ $log . warn ( 'Passing mixins and difinitions in the model method will be deprecated in restmod 1.2, use restmod.model().mix() instead.' ) ;
377377 }
378378
379379 return model ;
@@ -1231,6 +1231,43 @@ RMModule.factory('RMExtendedApi', ['$q', 'RMPackerCache', function($q, packerCac
12311231 }
12321232 } ;
12331233
1234+ } ] ) ;
1235+ RMModule . factory ( 'RMListApi' , [ function ( ) {
1236+
1237+ /**
1238+ * @class ListApi
1239+ *
1240+ * @description Common methods for Lists and Collections.
1241+ */
1242+ return {
1243+
1244+ /**
1245+ * @memberof ListApi#
1246+ *
1247+ * @description Generates a new list from this one.
1248+ *
1249+ * If called without arguments, the list is popupated with the same contents as this list.
1250+ *
1251+ * If there is a pending async operation on the host collection/list, then this method will
1252+ * return an empty list and fill it when the async operation finishes. If you don't need the async behavior
1253+ * then use `$type.list` directly to generate a new list.
1254+ *
1255+ * @param {function } _filter A filter function that should return the list contents as an array.
1256+ * @return {ListApi } list
1257+ */
1258+ $asList : function ( _filter ) {
1259+ var list = this . $type . list ( ) ,
1260+ promise = this . $asPromise ( ) ;
1261+
1262+ // set the list initial promise to the resolution of the parent promise.
1263+ list . $promise = promise . then ( function ( _this ) {
1264+ list . push . apply ( list , _filter ? _filter ( _this ) : _this ) ;
1265+ } ) ;
1266+
1267+ return list ;
1268+ }
1269+ } ;
1270+
12341271} ] ) ;
12351272RMModule . factory ( 'RMRecordApi' , [ 'RMUtils' , function ( Utils ) {
12361273
@@ -1524,7 +1561,16 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
15241561 method : this . $type . getProperty ( 'patchMethod' , 'PATCH' ) , // allow user to override patch method
15251562 url : url ,
15261563 // Use special mask for patches, mask everything that is not in the patch list.
1527- data : this . $wrap ( function ( _name ) { return _patch . indexOf ( _name ) === - 1 ; } )
1564+ data : this . $wrap ( function ( _name ) {
1565+ for ( var i = 0 , l = _patch . length ; i < l ; i ++ ) {
1566+ if ( _name === _patch [ i ] ||
1567+ _name . indexOf ( _patch [ i ] + '.' ) === 0 ||
1568+ _patch [ i ] . indexOf ( _name + '.' ) === 0
1569+ ) { return false ; }
1570+ }
1571+
1572+ return true ;
1573+ } )
15281574 } ;
15291575 } else {
15301576 request = { method : 'PUT' , url : url , data : this . $wrap ( Utils . UPDATE_MASK ) } ;
@@ -1781,74 +1827,6 @@ RMModule.factory('RMScopeApi', ['RMUtils', function(Utils) {
17811827 }
17821828 } ;
17831829
1784- } ] ) ;
1785- RMModule . factory ( 'RMViewApi' , [ function ( ) {
1786-
1787- /**
1788- * @class CollectionApi
1789- *
1790- * @extends ScopeApi
1791- * @extends CommonApi
1792- *
1793- * @description
1794- *
1795- * A restmod collection is an extended array type bound REST resource route.
1796- *
1797- * Every time a new restmod model is created, an associated collection type is created too.
1798- *
1799- * TODO: talk about fetch/refresh behaviour, lifecycles, collection scopes, adding/removing
1800- *
1801- * For `$fetch` on a collection:
1802- *
1803- * * before-fetch-many
1804- * * before-request
1805- * * after-request[-error]
1806- * * after-feed (called for every record if no errors)
1807- * * after-feed-many (only called if no errors)
1808- * * after-fetch-many[-error]
1809- *
1810- * @property {boolean } $isCollection Helper flag to separate collections from the main type
1811- * @property {object } $scope The collection scope (hierarchical scope, not angular scope)
1812- * @property {object } $params The collection query parameters
1813- *
1814- */
1815- var API = {
1816-
1817- $isCollection : true ,
1818-
1819- /**
1820- * @memberof CollectionApi#
1821- *
1822- * @description Called by collection constructor on initialization.
1823- *
1824- * Note: Is better to add a hook on after-init than overriding this method.
1825- */
1826- $initialize : function ( ) {
1827-
1828- this . $collection . on ( 'after-add' , function ( ) {
1829-
1830- } ) . on ( 'after-remove' , function ( ) {
1831-
1832- } ) . on ( 'after-clear' , function ( ) {
1833-
1834- } ) ;
1835- } ,
1836-
1837- $reload : function ( ) {
1838-
1839- }
1840- } ;
1841-
1842- // Proxy common collection methods to collection
1843-
1844- angular . forEach ( [ '$fetch' , '$create' , '$new' , '$build' , '$buildRaw' ] , function ( _method ) {
1845- API [ _method ] = function ( ) {
1846- return this . $collection [ _method ] . apply ( this . $collection , arguments ) ;
1847- } ;
1848- } ) ;
1849-
1850- return API ;
1851-
18521830} ] ) ;
18531831RMModule . factory ( 'RMBuilder' , [ '$injector' , 'inflector' , '$log' , 'RMUtils' , function ( $injector , inflector , $log , Utils ) {
18541832
@@ -2711,8 +2689,8 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
27112689 } ) ;
27122690
27132691} ] ) ;
2714- RMModule . factory ( 'RMModelFactory' , [ '$injector' , 'inflector' , 'RMUtils' , 'RMScopeApi' , 'RMCommonApi' , 'RMRecordApi' , 'RMCollectionApi' , 'RMExtendedApi' , 'RMSerializer' , 'RMBuilder' ,
2715- function ( $injector , inflector , Utils , ScopeApi , CommonApi , RecordApi , CollectionApi , ExtendedApi , Serializer , Builder ) {
2692+ RMModule . factory ( 'RMModelFactory' , [ '$injector' , 'inflector' , 'RMUtils' , 'RMScopeApi' , 'RMCommonApi' , 'RMRecordApi' , 'RMListApi' , ' RMCollectionApi', 'RMExtendedApi' , 'RMSerializer' , 'RMBuilder' ,
2693+ function ( $injector , inflector , Utils , ScopeApi , CommonApi , RecordApi , ListApi , CollectionApi , ExtendedApi , Serializer , Builder ) {
27162694
27172695 var NAME_RGX = / ( .* ?) ( [ ^ \/ ] + ) \/ ? $ / ,
27182696 extend = Utils . extendOverriden ;
@@ -2752,15 +2730,16 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
27522730 }
27532731
27542732 var Collection = Utils . buildArrayType ( ) ,
2733+ List = Utils . buildArrayType ( ) ,
27552734 Dummy = function ( _asCollection ) {
27562735 this . $isCollection = _asCollection ;
2757- this . $initialize ( ) ;
2736+ this . $initialize ( ) ; // TODO: deprecate this
27582737 } ;
27592738
2760- // Collection factory (since a constructor cant be provided...)
2761- function newCollection ( _scope , _params ) {
2739+ // Collection factory
2740+ function newCollection ( _params , _scope ) {
27622741 var col = new Collection ( ) ;
2763- col . $scope = _scope ;
2742+ col . $scope = _scope || Model ;
27642743 col . $params = _params ;
27652744 col . $initialize ( ) ;
27662745 return col ;
@@ -2804,9 +2783,7 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
28042783 } ,
28052784
28062785 // creates a new collection bound by default to the static scope
2807- $collection : function ( _params , _scope ) {
2808- return newCollection ( _scope || Model , _params ) ;
2809- } ,
2786+ $collection : newCollection ,
28102787
28112788 // gets scope url
28122789 $url : function ( ) {
@@ -2914,6 +2891,21 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
29142891 return new Dummy ( _asCollection ) ;
29152892 } ,
29162893
2894+ /**
2895+ * Creates a new record list.
2896+ *
2897+ * A list is a ordered set of records not bound to a particular scope.
2898+ *
2899+ * Contained records can belong to any scope.
2900+ *
2901+ * @return {List } the new list
2902+ */
2903+ list : function ( _items ) {
2904+ var list = new List ( ) ;
2905+ if ( _items ) list . push . apply ( list , _items ) ;
2906+ return list ;
2907+ } ,
2908+
29172909 /**
29182910 * @memberof StaticApi#
29192911 *
@@ -3072,10 +3064,18 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
30723064 // provide collection constructor
30733065 $collection : function ( _params , _scope ) {
30743066 _params = this . $params ? angular . extend ( { } , this . $params , _params ) : _params ;
3075- return newCollection ( _scope || this . $scope , _params ) ;
3067+ return newCollection ( _params , _scope || this . $scope ) ;
30763068 }
30773069
3078- } , ScopeApi , CommonApi , CollectionApi , ExtendedApi ) ;
3070+ } , ListApi , ScopeApi , CommonApi , CollectionApi , ExtendedApi ) ;
3071+
3072+ ///// Setup list api
3073+
3074+ extend ( List . prototype , {
3075+
3076+ $type : Model
3077+
3078+ } , ListApi , CommonApi ) ;
30793079
30803080 ///// Setup dummy api
30813081
@@ -3095,6 +3095,7 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
30953095 Model : Model ,
30963096 Record : Model . prototype ,
30973097 Collection : Collection . prototype ,
3098+ List : List . prototype ,
30983099 Dummy : Dummy . prototype
30993100 } ;
31003101
@@ -3242,13 +3243,18 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
32423243
32433244 switch ( api ) {
32443245 // Virtual API's
3246+ case 'List' :
3247+ helpDefine ( 'Collection' , name , _fun ) ;
3248+ helpDefine ( 'List' , name , _fun ) ;
3249+ break ;
32453250 case 'Scope' :
32463251 helpDefine ( 'Model' , name , _fun ) ;
32473252 helpDefine ( 'Collection' , name , _fun ) ;
32483253 break ;
32493254 case 'Resource' :
32503255 helpDefine ( 'Record' , name , _fun ) ;
32513256 helpDefine ( 'Collection' , name , _fun ) ;
3257+ helpDefine ( 'List' , name , _fun ) ;
32523258 helpDefine ( 'Dummy' , name , _fun ) ;
32533259 break ;
32543260 default :
0 commit comments