@@ -107,22 +107,23 @@ var methodsToProxy = [
107107 */
108108function defineResource ( definition ) {
109109 var DS = this ;
110+ var DSUtils = DS . utils ;
110111 var definitions = DS . definitions ;
111112 var IA = DS . errors . IA ;
112113
113- if ( DS . utils . isString ( definition ) ) {
114+ if ( DSUtils . isString ( definition ) ) {
114115 definition = definition . replace ( / \s / gi, '' ) ;
115116 definition = {
116117 name : definition
117118 } ;
118119 }
119- if ( ! DS . utils . isObject ( definition ) ) {
120+ if ( ! DSUtils . isObject ( definition ) ) {
120121 throw new IA ( errorPrefix + 'definition: Must be an object!' ) ;
121- } else if ( ! DS . utils . isString ( definition . name ) ) {
122+ } else if ( ! DSUtils . isString ( definition . name ) ) {
122123 throw new IA ( errorPrefix + 'definition.name: Must be a string!' ) ;
123- } else if ( definition . idAttribute && ! DS . utils . isString ( definition . idAttribute ) ) {
124+ } else if ( definition . idAttribute && ! DSUtils . isString ( definition . idAttribute ) ) {
124125 throw new IA ( errorPrefix + 'definition.idAttribute: Must be a string!' ) ;
125- } else if ( definition . endpoint && ! DS . utils . isString ( definition . endpoint ) ) {
126+ } else if ( definition . endpoint && ! DSUtils . isString ( definition . endpoint ) ) {
126127 throw new IA ( errorPrefix + 'definition.endpoint: Must be a string!' ) ;
127128 } else if ( DS . store [ definition . name ] ) {
128129 throw new DS . errors . R ( errorPrefix + definition . name + ' is already registered!' ) ;
@@ -131,20 +132,20 @@ function defineResource(definition) {
131132 try {
132133 // Inherit from global defaults
133134 Resource . prototype = DS . defaults ;
134- definitions [ definition . name ] = new Resource ( DS . utils , definition ) ;
135+ definitions [ definition . name ] = new Resource ( DSUtils , definition ) ;
135136
136137 var def = definitions [ definition . name ] ;
137138
138139 // Setup nested parent configuration
139140 if ( def . relations ) {
140141 def . relationList = [ ] ;
141142 def . relationFields = [ ] ;
142- DS . utils . forOwn ( def . relations , function ( relatedModels , type ) {
143- DS . utils . forOwn ( relatedModels , function ( defs , relationName ) {
144- if ( ! DS . utils . isArray ( defs ) ) {
143+ DSUtils . forOwn ( def . relations , function ( relatedModels , type ) {
144+ DSUtils . forOwn ( relatedModels , function ( defs , relationName ) {
145+ if ( ! DSUtils . isArray ( defs ) ) {
145146 relatedModels [ relationName ] = [ defs ] ;
146147 }
147- DS . utils . forEach ( relatedModels [ relationName ] , function ( d ) {
148+ DSUtils . forEach ( relatedModels [ relationName ] , function ( d ) {
148149 d . type = type ;
149150 d . relation = relationName ;
150151 d . name = def . name ;
@@ -154,17 +155,17 @@ function defineResource(definition) {
154155 } ) ;
155156 } ) ;
156157 if ( def . relations . belongsTo ) {
157- DS . utils . forOwn ( def . relations . belongsTo , function ( relatedModel , modelName ) {
158- DS . utils . forEach ( relatedModel , function ( relation ) {
158+ DSUtils . forOwn ( def . relations . belongsTo , function ( relatedModel , modelName ) {
159+ DSUtils . forEach ( relatedModel , function ( relation ) {
159160 if ( relation . parent ) {
160161 def . parent = modelName ;
161162 def . parentKey = relation . localKey ;
162163 }
163164 } ) ;
164165 } ) ;
165166 }
166- DS . utils . deepFreeze ( def . relations ) ;
167- DS . utils . deepFreeze ( def . relationList ) ;
167+ DSUtils . deepFreeze ( def . relations ) ;
168+ DSUtils . deepFreeze ( def . relationList ) ;
168169 }
169170
170171 def . getEndpoint = function ( attrs , options ) {
@@ -177,17 +178,17 @@ function defineResource(definition) {
177178 options = options || { } ;
178179 options . params = options . params || { } ;
179180 if ( parent && parentKey && definitions [ parent ] && options . params [ parentKey ] !== false ) {
180- if ( DS . utils . isNumber ( attrs ) || DS . utils . isString ( attrs ) ) {
181+ if ( DSUtils . isNumber ( attrs ) || DSUtils . isString ( attrs ) ) {
181182 item = DS . get ( this . name , attrs ) ;
182183 }
183- if ( DS . utils . isObject ( attrs ) && parentKey in attrs ) {
184+ if ( DSUtils . isObject ( attrs ) && parentKey in attrs ) {
184185 delete options . params [ parentKey ] ;
185- endpoint = DS . utils . makePath ( definitions [ parent ] . getEndpoint ( attrs , options ) , attrs [ parentKey ] , thisEndpoint ) ;
186+ endpoint = DSUtils . makePath ( definitions [ parent ] . getEndpoint ( attrs , options ) , attrs [ parentKey ] , thisEndpoint ) ;
186187 } else if ( item && parentKey in item ) {
187188 delete options . params [ parentKey ] ;
188- endpoint = DS . utils . makePath ( definitions [ parent ] . getEndpoint ( attrs , options ) , item [ parentKey ] , thisEndpoint ) ;
189+ endpoint = DSUtils . makePath ( definitions [ parent ] . getEndpoint ( attrs , options ) , item [ parentKey ] , thisEndpoint ) ;
189190 } else if ( options && options . params [ parentKey ] ) {
190- endpoint = DS . utils . makePath ( definitions [ parent ] . getEndpoint ( attrs , options ) , options . params [ parentKey ] , thisEndpoint ) ;
191+ endpoint = DSUtils . makePath ( definitions [ parent ] . getEndpoint ( attrs , options ) , options . params [ parentKey ] , thisEndpoint ) ;
191192 delete options . params [ parentKey ] ;
192193 }
193194 }
@@ -212,7 +213,7 @@ function defineResource(definition) {
212213 deleteOnExpire : def . deleteOnExpire || 'none' ,
213214 onExpire : function ( id ) {
214215 var item = DS . eject ( def . name , id ) ;
215- if ( DS . utils . isFunction ( def . onExpire ) ) {
216+ if ( DSUtils . isFunction ( def . onExpire ) ) {
216217 def . onExpire ( id , item ) ;
217218 }
218219 } ,
@@ -224,18 +225,18 @@ function defineResource(definition) {
224225 } ) ;
225226
226227 // Create the wrapper class for the new resource
227- def . class = DS . utils . pascalCase ( definition . name ) ;
228+ def . class = DSUtils . pascalCase ( definition . name ) ;
228229 eval ( 'function ' + def . class + '() {}' ) ;
229230 def [ def . class ] = eval ( def . class ) ;
230231
231232 // Apply developer-defined methods
232233 if ( def . methods ) {
233- DS . utils . deepMixIn ( def [ def . class ] . prototype , def . methods ) ;
234+ DSUtils . deepMixIn ( def [ def . class ] . prototype , def . methods ) ;
234235 }
235236
236237 // Prepare for computed properties
237238 if ( def . computed ) {
238- DS . utils . forOwn ( def . computed , function ( fn , field ) {
239+ DSUtils . forOwn ( def . computed , function ( fn , field ) {
239240 if ( angular . isFunction ( fn ) ) {
240241 def . computed [ field ] = [ fn ] ;
241242 fn = def . computed [ field ] ;
@@ -257,7 +258,7 @@ function defineResource(definition) {
257258 angular . forEach ( deps , function ( val , index ) {
258259 deps [ index ] = val . trim ( ) ;
259260 } ) ;
260- fn . deps = DS . utils . filter ( deps , function ( dep ) {
261+ fn . deps = DSUtils . filter ( deps , function ( dep ) {
261262 return ! ! dep ;
262263 } ) ;
263264 } ) ;
@@ -309,6 +310,9 @@ function defineResource(definition) {
309310 def . beforeDestroy = DS . $q . promisify ( def . beforeDestroy ) ;
310311 def . afterDestroy = DS . $q . promisify ( def . afterDestroy ) ;
311312
313+ // Mix-in events
314+ DSUtils . Events ( def ) ;
315+
312316 return def ;
313317 } catch ( err ) {
314318 DS . $log . error ( err ) ;
0 commit comments