@@ -2501,6 +2501,10 @@ BaseConfig.prototype.beforeUpdate = lifecycleNoop;
2501
2501
BaseConfig . prototype . afterUpdate = lifecycleNoop ;
2502
2502
BaseConfig . prototype . beforeDestroy = lifecycleNoop ;
2503
2503
BaseConfig . prototype . afterDestroy = lifecycleNoop ;
2504
+ BaseConfig . prototype . beforeInject = function ( ) {
2505
+ } ;
2506
+ BaseConfig . prototype . afterInject = function ( ) {
2507
+ } ;
2504
2508
2505
2509
/**
2506
2510
* @doc function
@@ -2531,6 +2535,8 @@ function DSProvider() {
2531
2535
* - `{function}` - `afterUpdate` - See [](). Default: No-op
2532
2536
* - `{function}` - `beforeDestroy` - See [](). Default: No-op
2533
2537
* - `{function}` - `afterDestroy` - See [](). Default: No-op
2538
+ * - `{function}` - `beforeInject` - See [](). Default: No-op
2539
+ * - `{function}` - `afterInject` - See [](). Default: No-op
2534
2540
*/
2535
2541
var defaults = this . defaults = new BaseConfig ( ) ;
2536
2542
@@ -2840,6 +2846,7 @@ function changes(resourceName, id) {
2840
2846
module . exports = changes ;
2841
2847
2842
2848
} , { } ] , 44 :[ function ( require , module , exports ) {
2849
+ /*jshint evil:true*/
2843
2850
var errorPrefix = 'DS.defineResource(definition): ' ;
2844
2851
2845
2852
function Resource ( utils , options ) {
@@ -2892,6 +2899,10 @@ function Resource(utils, options) {
2892
2899
* - `{string="id"}` - `idAttribute` - The attribute that specifies the primary key for this resource.
2893
2900
* - `{string=}` - `endpoint` - The attribute that specifies the primary key for this resource. Default is the value of `name`.
2894
2901
* - `{string=}` - `baseUrl` - The url relative to which all AJAX requests will be made.
2902
+ * - `{object=}` - `methods` - If provided, items of this resource will be wrapped in a constructor function that is
2903
+ * empty save for the attributes in this option which will be mixed in to the constructor function prototype. Enabling
2904
+ * this feature for this resource will incur a slight performance penalty, but allows you to give custom behavior to what
2905
+ * are now "instances" of this resource.
2895
2906
* - `{function=}` - `beforeValidate` - Lifecycle hook. Overrides global. Signature: `beforeValidate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2896
2907
* - `{function=}` - `validate` - Lifecycle hook. Overrides global. Signature: `validate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2897
2908
* - `{function=}` - `afterValidate` - Lifecycle hook. Overrides global. Signature: `afterValidate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
@@ -2901,6 +2912,8 @@ function Resource(utils, options) {
2901
2912
* - `{function=}` - `afterUpdate` - Lifecycle hook. Overrides global. Signature: `afterUpdate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2902
2913
* - `{function=}` - `beforeDestroy` - Lifecycle hook. Overrides global. Signature: `beforeDestroy(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2903
2914
* - `{function=}` - `afterDestroy` - Lifecycle hook. Overrides global. Signature: `afterDestroy(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2915
+ * - `{function=}` - `beforeInject` - Lifecycle hook. Overrides global. Signature: `beforeInject(resourceName, attrs)`.
2916
+ * - `{function=}` - `afterInject` - Lifecycle hook. Overrides global. Signature: `afterInject(resourceName, attrs)`.
2904
2917
*/
2905
2918
function defineResource ( definition ) {
2906
2919
if ( this . utils . isString ( definition ) ) {
@@ -2924,24 +2937,32 @@ function defineResource(definition) {
2924
2937
Resource . prototype = this . defaults ;
2925
2938
this . definitions [ definition . name ] = new Resource ( this . utils , definition ) ;
2926
2939
2927
- var _this = this ;
2940
+ var _this = this ,
2941
+ def = this . definitions [ definition . name ] ;
2928
2942
2929
- var cache = this . cacheFactory ( 'DS.' + definition . name , {
2930
- maxAge : definition . maxAge || null ,
2931
- recycleFreq : definition . recycleFreq || 1000 ,
2932
- cacheFlushInterval : definition . cacheFlushInterval || null ,
2933
- deleteOnExpire : definition . deleteOnExpire || 'none' ,
2943
+ var cache = this . cacheFactory ( 'DS.' + def . name , {
2944
+ maxAge : def . maxAge || null ,
2945
+ recycleFreq : def . recycleFreq || 1000 ,
2946
+ cacheFlushInterval : def . cacheFlushInterval || null ,
2947
+ deleteOnExpire : def . deleteOnExpire || 'none' ,
2934
2948
onExpire : function ( id ) {
2935
- _this . eject ( definition . name , id ) ;
2949
+ _this . eject ( def . name , id ) ;
2936
2950
} ,
2937
2951
capacity : Number . MAX_VALUE ,
2938
2952
storageMode : 'memory' ,
2939
2953
storageImpl : null ,
2940
2954
disabled : false ,
2941
- storagePrefix : 'DS.' + definition . name
2955
+ storagePrefix : 'DS.' + def . name
2942
2956
} ) ;
2943
2957
2944
- this . store [ definition . name ] = {
2958
+ if ( def . methods ) {
2959
+ def . class = definition . name [ 0 ] . toUpperCase ( ) + definition . name . substring ( 1 ) ;
2960
+ eval ( 'function ' + def . class + '() {}' ) ;
2961
+ def [ def . class ] = eval ( def . class ) ;
2962
+ this . utils . deepMixIn ( def [ def . class ] . prototype , def . methods ) ;
2963
+ }
2964
+
2965
+ this . store [ def . name ] = {
2945
2966
collection : [ ] ,
2946
2967
completedQueries : { } ,
2947
2968
pendingQueries : { } ,
@@ -3648,11 +3669,12 @@ function _inject(definition, resource, attrs) {
3648
3669
if ( ! ( definition . idAttribute in attrs ) ) {
3649
3670
throw new _this . errors . RuntimeError ( errorPrefix + 'attrs: Must contain the property specified by `idAttribute`!' ) ;
3650
3671
} else {
3672
+ definition . beforeInject ( definition . name , attrs ) ;
3651
3673
var id = attrs [ definition . idAttribute ] ,
3652
3674
item = this . get ( definition . name , id ) ;
3653
3675
3654
3676
if ( ! item ) {
3655
- item = { } ;
3677
+ item = definition . class ? new definition [ definition . class ] ( ) : { } ;
3656
3678
resource . previousAttributes [ id ] = { } ;
3657
3679
3658
3680
_this . utils . deepMixIn ( item , attrs ) ;
@@ -3676,6 +3698,7 @@ function _inject(definition, resource, attrs) {
3676
3698
resource . observers [ id ] . deliver ( ) ;
3677
3699
}
3678
3700
resource . saved [ id ] = _this . utils . updateTimestamp ( resource . saved [ id ] ) ;
3701
+ definition . afterInject ( definition . name , item ) ;
3679
3702
}
3680
3703
}
3681
3704
}
@@ -3748,7 +3771,11 @@ function inject(resourceName, attrs, options) {
3748
3771
} else {
3749
3772
_inject . apply ( _this , [ definition , resource , attrs ] ) ;
3750
3773
}
3751
- return attrs ;
3774
+ if ( _this . utils . isArray ( attrs ) ) {
3775
+ return attrs ;
3776
+ } else {
3777
+ return this . get ( resourceName , attrs [ definition . idAttribute ] ) ;
3778
+ }
3752
3779
} catch ( err ) {
3753
3780
if ( ! ( err instanceof this . errors . RuntimeError ) ) {
3754
3781
throw new this . errors . UnhandledError ( err ) ;
0 commit comments