1
1
/**
2
2
* API Bound Models for AngularJS
3
- * @version v1.1.2 - 2014-09-24
3
+ * @version v1.1.3 - 2014-09-25
4
4
* @link https://github.com/angular-platanus/restmod
5
5
* @author Ignacio Baixas <[email protected] >
6
6
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -1002,42 +1002,32 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
1002
1002
this . $status = 'pending' ;
1003
1003
this . $dispatch ( 'before-request' , [ _options ] ) ;
1004
1004
1005
- var dsp = this . $dispatcher ( ) , _this = this ;
1006
- return $http ( _options ) . then ( function ( _response ) {
1007
-
1008
- return _this . $decorate ( dsp , function ( ) {
1009
- if ( action && action . canceled ) {
1010
- // if request was canceled during request, ignore post request actions.
1011
- this . $status = 'canceled' ;
1012
- } else {
1013
- this . $status = 'ok' ;
1014
- this . $response = _response ;
1015
-
1016
- this . $dispatch ( 'after-request' , [ _response ] ) ;
1017
- if ( _success ) _success . call ( this , _response ) ;
1018
- }
1019
- } ) ;
1020
-
1021
- } , function ( _response ) {
1022
-
1023
- return _this . $decorate ( dsp , function ( ) {
1024
- if ( action && action . canceled ) {
1025
- // if request was canceled during request, ignore error handling
1026
- this . $status = 'canceled' ;
1027
- return this ;
1028
- } else {
1029
- this . $status = 'error' ;
1030
- this . $response = _response ;
1005
+ return $http ( _options ) . then ( wrapPromise ( this , function ( ) {
1006
+ if ( action && action . canceled ) {
1007
+ // if request was canceled during request, ignore post request actions.
1008
+ this . $status = 'canceled' ;
1009
+ } else {
1010
+ this . $status = 'ok' ;
1011
+ this . $response = this . $last ;
1012
+ this . $dispatch ( 'after-request' , [ this . $last ] ) ;
1013
+ if ( _success ) _success . call ( this , this . $last ) ;
1014
+ }
1015
+ } ) , wrapPromise ( this , function ( ) {
1016
+ if ( action && action . canceled ) {
1017
+ // if request was canceled during request, ignore error handling
1018
+ this . $status = 'canceled' ;
1019
+ } else {
1020
+ this . $status = 'error' ;
1021
+ this . $response = this . $last ;
1031
1022
1032
- // IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
1033
- // until the error flag is reset by user.
1023
+ // IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
1024
+ // until the error flag is reset by user.
1034
1025
1035
- this . $dispatch ( 'after-request-error' , [ _response ] ) ;
1036
- if ( _error ) _error . call ( this , _response ) ;
1037
- return $q . reject ( this ) ;
1038
- }
1039
- } ) ;
1040
- } ) ;
1026
+ this . $dispatch ( 'after-request-error' , [ this . $last ] ) ;
1027
+ if ( _error ) _error . call ( this , this . $last ) ;
1028
+ return $q . reject ( this ) ; // TODO: this will step over any promise generated in _error!!
1029
+ }
1030
+ } ) ) ;
1041
1031
} ) ;
1042
1032
} ,
1043
1033
@@ -2230,6 +2220,29 @@ RMModule.factory('RMBuilderExt', ['$injector', '$parse', 'inflector', '$log', 'r
2230
2220
} ] ) ;
2231
2221
RMModule . factory ( 'RMBuilderRelations' , [ '$injector' , 'inflector' , '$log' , 'RMUtils' , 'restmod' , 'RMPackerCache' , function ( $injector , inflector , $log , Utils , restmod , packerCache ) {
2232
2222
2223
+ // hooks: only has_many/has_one
2224
+ // support global hooks using config.
2225
+
2226
+ function wrapHook ( _fun , _owner ) {
2227
+ return function ( ) {
2228
+ var oldOwner = this . $owner ;
2229
+ this . $owner = _owner ;
2230
+ try {
2231
+ return _fun . apply ( this , arguments ) ;
2232
+ } finally {
2233
+ this . $owner = oldOwner ;
2234
+ }
2235
+ } ;
2236
+ }
2237
+
2238
+ function applyHooks ( _target , _hooks , _owner ) {
2239
+ for ( var key in _hooks ) {
2240
+ if ( _hooks . hasOwnProperty ( key ) ) {
2241
+ _target . $on ( key , wrapHook ( _hooks [ key ] , _owner ) ) ;
2242
+ }
2243
+ }
2244
+ }
2245
+
2233
2246
/**
2234
2247
* @class RelationBuilderApi
2235
2248
*
@@ -2257,13 +2270,19 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
2257
2270
* @param {string } _inverseOf Inverse property name (optional)
2258
2271
* @return {BuilderApi } self
2259
2272
*/
2260
- attrAsCollection : function ( _attr , _model , _url , _source , _inverseOf ) {
2273
+ attrAsCollection : function ( _attr , _model , _url , _source , _inverseOf , _params , _decorateScope , _hooks ) {
2274
+
2275
+ var globalHooks ;
2261
2276
2262
2277
this . attrDefault ( _attr , function ( ) {
2263
2278
2264
2279
if ( typeof _model === 'string' ) {
2265
2280
_model = $injector . get ( _model ) ;
2266
2281
2282
+ // retrieve global options
2283
+ if ( ! _decorateScope ) _decorateScope = _model . getProperty ( 'hasManyOpt' ) ;
2284
+ globalHooks = _model . getProperty ( 'hasManyOpt' ) ;
2285
+
2267
2286
if ( _inverseOf ) {
2268
2287
var desc = _model . $$getDescription ( _inverseOf ) ;
2269
2288
if ( ! desc || desc . relation !== 'belongs_to' ) {
@@ -2273,25 +2292,23 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
2273
2292
}
2274
2293
}
2275
2294
2276
- var self = this ,
2277
- scope = this . $buildScope ( _model , _url || inflector . parameterize ( _attr ) ) ,
2278
- col = _model . $collection ( null , scope ) ;
2295
+ var scope = this . $buildScope ( _model , _url || inflector . parameterize ( _attr ) ) , col ;
2279
2296
2280
- // TODO: there should be a way to modify scope behavior just for this relation,
2281
- // since relation item scope IS the collection, then the collection should
2282
- // be extended to provide a modified scope. For this an additional _extensions
2283
- // parameters could be added to collection, then these 'extensions' are inherited
2284
- // by child collections, the other alternative is to enable full property inheritance ...
2297
+ // setup collection
2298
+ if ( _decorateScope ) scope = _decorateScope . call ( this , scope ) ;
2299
+ col = _model . $collection ( _params || null , scope ) ;
2300
+ if ( globalHooks ) applyHooks ( col , globalHooks , this ) ;
2301
+ if ( _hooks ) applyHooks ( col , _hooks , this ) ;
2285
2302
2286
2303
// set inverse property if required.
2287
2304
if ( _inverseOf ) {
2305
+ var self = this ;
2288
2306
col . $on ( 'after-add' , function ( _obj ) {
2289
2307
_obj [ _inverseOf ] = self ;
2290
2308
} ) ;
2291
2309
}
2292
2310
2293
2311
return col ;
2294
- // simple support for inline data, TODO: maybe deprecate this.
2295
2312
} ) ;
2296
2313
2297
2314
if ( _source || _url ) this . attrMap ( _attr , _source || _url ) ;
@@ -2317,13 +2334,19 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
2317
2334
* @param {string } _inverseOf Inverse property name (optional)
2318
2335
* @return {BuilderApi } self
2319
2336
*/
2320
- attrAsResource : function ( _attr , _model , _url , _source , _inverseOf ) {
2337
+ attrAsResource : function ( _attr , _model , _url , _source , _inverseOf , _decorateScope , _hooks ) {
2338
+
2339
+ var globalHooks ;
2321
2340
2322
2341
this . attrDefault ( _attr , function ( ) {
2323
2342
2324
2343
if ( typeof _model === 'string' ) {
2325
2344
_model = $injector . get ( _model ) ;
2326
2345
2346
+ // retrieve global options
2347
+ if ( ! _decorateScope ) _decorateScope = _model . getProperty ( 'hasOneOpt' ) ;
2348
+ globalHooks = _model . getProperty ( 'hasOne' ) ;
2349
+
2327
2350
if ( _inverseOf ) {
2328
2351
var desc = _model . $$getDescription ( _inverseOf ) ;
2329
2352
if ( ! desc || desc . relation !== 'belongs_to' ) {
@@ -2333,10 +2356,13 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
2333
2356
}
2334
2357
}
2335
2358
2336
- var scope = this . $buildScope ( _model , _url || inflector . parameterize ( _attr ) ) ,
2337
- inst = _model . $new ( null , scope ) ;
2359
+ var scope = this . $buildScope ( _model , _url || inflector . parameterize ( _attr ) ) , inst ;
2338
2360
2339
- // TODO: provide a way to modify scope behavior just for this relation
2361
+ // setup record
2362
+ if ( _decorateScope ) scope = _decorateScope . call ( this , scope ) ;
2363
+ inst = _model . $new ( null , scope ) ;
2364
+ if ( globalHooks ) applyHooks ( inst , globalHooks , this ) ;
2365
+ if ( _hooks ) applyHooks ( inst , _hooks , this ) ;
2340
2366
2341
2367
if ( _inverseOf ) {
2342
2368
inst [ _inverseOf ] = this ;
@@ -2553,8 +2579,8 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
2553
2579
} ;
2554
2580
2555
2581
return restmod . mixin ( function ( ) {
2556
- this . extend ( 'attrAsCollection' , EXT . attrAsCollection , [ 'hasMany' , 'path' , 'source' , 'inverseOf' ] ) // TODO: rename source to map, but disable attrMap if map is used here...
2557
- . extend ( 'attrAsResource' , EXT . attrAsResource , [ 'hasOne' , 'path' , 'source' , 'inverseOf' ] )
2582
+ this . extend ( 'attrAsCollection' , EXT . attrAsCollection , [ 'hasMany' , 'path' , 'source' , 'inverseOf' , 'params' , 'scope' , 'hooks' ] ) // TODO: rename source to map, but disable attrMap if map is used here...
2583
+ . extend ( 'attrAsResource' , EXT . attrAsResource , [ 'hasOne' , 'path' , 'source' , 'inverseOf' , 'scope' , 'hooks' ] )
2558
2584
. extend ( 'attrAsReference' , EXT . attrAsReference , [ 'belongsTo' , 'key' , 'prefetch' ] )
2559
2585
. extend ( 'attrAsReferenceToMany' , EXT . attrAsReferenceToMany , [ 'belongsToMany' , 'keys' ] ) ;
2560
2586
} ) ;
0 commit comments