11/**
22 * API Bound Models for AngularJS
3- * @version v1.1.2 - 2014-09-24
3+ * @version v1.1.3 - 2014-09-25
44 * @link https://github.com/angular-platanus/restmod
55 * @author Ignacio Baixas <[email protected] > 66 * @license MIT License, http://www.opensource.org/licenses/MIT
@@ -1002,42 +1002,32 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
10021002 this . $status = 'pending' ;
10031003 this . $dispatch ( 'before-request' , [ _options ] ) ;
10041004
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 ;
10311022
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.
10341025
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+ } ) ) ;
10411031 } ) ;
10421032 } ,
10431033
@@ -2230,6 +2220,29 @@ RMModule.factory('RMBuilderExt', ['$injector', '$parse', 'inflector', '$log', 'r
22302220} ] ) ;
22312221RMModule . factory ( 'RMBuilderRelations' , [ '$injector' , 'inflector' , '$log' , 'RMUtils' , 'restmod' , 'RMPackerCache' , function ( $injector , inflector , $log , Utils , restmod , packerCache ) {
22322222
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+
22332246 /**
22342247 * @class RelationBuilderApi
22352248 *
@@ -2257,13 +2270,19 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
22572270 * @param {string } _inverseOf Inverse property name (optional)
22582271 * @return {BuilderApi } self
22592272 */
2260- attrAsCollection : function ( _attr , _model , _url , _source , _inverseOf ) {
2273+ attrAsCollection : function ( _attr , _model , _url , _source , _inverseOf , _params , _decorateScope , _hooks ) {
2274+
2275+ var globalHooks ;
22612276
22622277 this . attrDefault ( _attr , function ( ) {
22632278
22642279 if ( typeof _model === 'string' ) {
22652280 _model = $injector . get ( _model ) ;
22662281
2282+ // retrieve global options
2283+ if ( ! _decorateScope ) _decorateScope = _model . getProperty ( 'hasManyOpt' ) ;
2284+ globalHooks = _model . getProperty ( 'hasManyOpt' ) ;
2285+
22672286 if ( _inverseOf ) {
22682287 var desc = _model . $$getDescription ( _inverseOf ) ;
22692288 if ( ! desc || desc . relation !== 'belongs_to' ) {
@@ -2273,25 +2292,23 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
22732292 }
22742293 }
22752294
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 ;
22792296
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 ) ;
22852302
22862303 // set inverse property if required.
22872304 if ( _inverseOf ) {
2305+ var self = this ;
22882306 col . $on ( 'after-add' , function ( _obj ) {
22892307 _obj [ _inverseOf ] = self ;
22902308 } ) ;
22912309 }
22922310
22932311 return col ;
2294- // simple support for inline data, TODO: maybe deprecate this.
22952312 } ) ;
22962313
22972314 if ( _source || _url ) this . attrMap ( _attr , _source || _url ) ;
@@ -2317,13 +2334,19 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
23172334 * @param {string } _inverseOf Inverse property name (optional)
23182335 * @return {BuilderApi } self
23192336 */
2320- attrAsResource : function ( _attr , _model , _url , _source , _inverseOf ) {
2337+ attrAsResource : function ( _attr , _model , _url , _source , _inverseOf , _decorateScope , _hooks ) {
2338+
2339+ var globalHooks ;
23212340
23222341 this . attrDefault ( _attr , function ( ) {
23232342
23242343 if ( typeof _model === 'string' ) {
23252344 _model = $injector . get ( _model ) ;
23262345
2346+ // retrieve global options
2347+ if ( ! _decorateScope ) _decorateScope = _model . getProperty ( 'hasOneOpt' ) ;
2348+ globalHooks = _model . getProperty ( 'hasOne' ) ;
2349+
23272350 if ( _inverseOf ) {
23282351 var desc = _model . $$getDescription ( _inverseOf ) ;
23292352 if ( ! desc || desc . relation !== 'belongs_to' ) {
@@ -2333,10 +2356,13 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
23332356 }
23342357 }
23352358
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 ;
23382360
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 ) ;
23402366
23412367 if ( _inverseOf ) {
23422368 inst [ _inverseOf ] = this ;
@@ -2553,8 +2579,8 @@ RMModule.factory('RMBuilderRelations', ['$injector', 'inflector', '$log', 'RMUti
25532579 } ;
25542580
25552581 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' ] )
25582584 . extend ( 'attrAsReference' , EXT . attrAsReference , [ 'belongsTo' , 'key' , 'prefetch' ] )
25592585 . extend ( 'attrAsReferenceToMany' , EXT . attrAsReferenceToMany , [ 'belongsToMany' , 'keys' ] ) ;
25602586 } ) ;
0 commit comments