@@ -2501,6 +2501,10 @@ BaseConfig.prototype.beforeUpdate = lifecycleNoop;
25012501BaseConfig . prototype . afterUpdate = lifecycleNoop ;
25022502BaseConfig . prototype . beforeDestroy = lifecycleNoop ;
25032503BaseConfig . prototype . afterDestroy = lifecycleNoop ;
2504+ BaseConfig . prototype . beforeInject = function ( ) {
2505+ } ;
2506+ BaseConfig . prototype . afterInject = function ( ) {
2507+ } ;
25042508
25052509/**
25062510 * @doc function
@@ -2531,6 +2535,8 @@ function DSProvider() {
25312535 * - `{function}` - `afterUpdate` - See [](). Default: No-op
25322536 * - `{function}` - `beforeDestroy` - See [](). Default: No-op
25332537 * - `{function}` - `afterDestroy` - See [](). Default: No-op
2538+ * - `{function}` - `beforeInject` - See [](). Default: No-op
2539+ * - `{function}` - `afterInject` - See [](). Default: No-op
25342540 */
25352541 var defaults = this . defaults = new BaseConfig ( ) ;
25362542
@@ -2892,6 +2898,10 @@ function Resource(utils, options) {
28922898 * - `{string="id"}` - `idAttribute` - The attribute that specifies the primary key for this resource.
28932899 * - `{string=}` - `endpoint` - The attribute that specifies the primary key for this resource. Default is the value of `name`.
28942900 * - `{string=}` - `baseUrl` - The url relative to which all AJAX requests will be made.
2901+ * - `{object=}` - `methods` - If provided, items of this resource will be wrapped in a constructor function that is
2902+ * empty save for the attributes in this option which will be mixed in to the constructor function prototype. Enabling
2903+ * this feature for this resource will incur a slight performance penalty, but allows you to give custom behavior to what
2904+ * are now "instances" of this resource.
28952905 * - `{function=}` - `beforeValidate` - Lifecycle hook. Overrides global. Signature: `beforeValidate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
28962906 * - `{function=}` - `validate` - Lifecycle hook. Overrides global. Signature: `validate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
28972907 * - `{function=}` - `afterValidate` - Lifecycle hook. Overrides global. Signature: `afterValidate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
@@ -2901,6 +2911,8 @@ function Resource(utils, options) {
29012911 * - `{function=}` - `afterUpdate` - Lifecycle hook. Overrides global. Signature: `afterUpdate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
29022912 * - `{function=}` - `beforeDestroy` - Lifecycle hook. Overrides global. Signature: `beforeDestroy(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
29032913 * - `{function=}` - `afterDestroy` - Lifecycle hook. Overrides global. Signature: `afterDestroy(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2914+ * - `{function=}` - `beforeInject` - Lifecycle hook. Overrides global. Signature: `beforeInject(resourceName, attrs)`.
2915+ * - `{function=}` - `afterInject` - Lifecycle hook. Overrides global. Signature: `afterInject(resourceName, attrs)`.
29042916 */
29052917function defineResource ( definition ) {
29062918 if ( this . utils . isString ( definition ) ) {
@@ -2924,24 +2936,31 @@ function defineResource(definition) {
29242936 Resource . prototype = this . defaults ;
29252937 this . definitions [ definition . name ] = new Resource ( this . utils , definition ) ;
29262938
2927- var _this = this ;
2939+ var _this = this ,
2940+ def = this . definitions [ definition . name ] ;
29282941
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' ,
2942+ var cache = this . cacheFactory ( 'DS.' + def . name , {
2943+ maxAge : def . maxAge || null ,
2944+ recycleFreq : def . recycleFreq || 1000 ,
2945+ cacheFlushInterval : def . cacheFlushInterval || null ,
2946+ deleteOnExpire : def . deleteOnExpire || 'none' ,
29342947 onExpire : function ( id ) {
2935- _this . eject ( definition . name , id ) ;
2948+ _this . eject ( def . name , id ) ;
29362949 } ,
29372950 capacity : Number . MAX_VALUE ,
29382951 storageMode : 'memory' ,
29392952 storageImpl : null ,
29402953 disabled : false ,
2941- storagePrefix : 'DS.' + definition . name
2954+ storagePrefix : 'DS.' + def . name
29422955 } ) ;
29432956
2944- this . store [ definition . name ] = {
2957+ if ( def . methods ) {
2958+ def . factory = function ( ) {
2959+ } ;
2960+ this . utils . deepMixIn ( def . factory . prototype , def . methods ) ;
2961+ }
2962+
2963+ this . store [ def . name ] = {
29452964 collection : [ ] ,
29462965 completedQueries : { } ,
29472966 pendingQueries : { } ,
@@ -3648,11 +3667,12 @@ function _inject(definition, resource, attrs) {
36483667 if ( ! ( definition . idAttribute in attrs ) ) {
36493668 throw new _this . errors . RuntimeError ( errorPrefix + 'attrs: Must contain the property specified by `idAttribute`!' ) ;
36503669 } else {
3670+ definition . beforeInject ( definition . name , attrs ) ;
36513671 var id = attrs [ definition . idAttribute ] ,
36523672 item = this . get ( definition . name , id ) ;
36533673
36543674 if ( ! item ) {
3655- item = { } ;
3675+ item = definition . factory ? new definition . factory ( ) : { } ;
36563676 resource . previousAttributes [ id ] = { } ;
36573677
36583678 _this . utils . deepMixIn ( item , attrs ) ;
@@ -3676,6 +3696,7 @@ function _inject(definition, resource, attrs) {
36763696 resource . observers [ id ] . deliver ( ) ;
36773697 }
36783698 resource . saved [ id ] = _this . utils . updateTimestamp ( resource . saved [ id ] ) ;
3699+ definition . afterInject ( definition . name , item ) ;
36793700 }
36803701 }
36813702}
@@ -3748,7 +3769,11 @@ function inject(resourceName, attrs, options) {
37483769 } else {
37493770 _inject . apply ( _this , [ definition , resource , attrs ] ) ;
37503771 }
3751- return attrs ;
3772+ if ( _this . utils . isArray ( attrs ) ) {
3773+ return attrs ;
3774+ } else {
3775+ return this . get ( resourceName , attrs [ definition . idAttribute ] ) ;
3776+ }
37523777 } catch ( err ) {
37533778 if ( ! ( err instanceof this . errors . RuntimeError ) ) {
37543779 throw new this . errors . UnhandledError ( err ) ;
0 commit comments