@@ -107,22 +107,23 @@ var methodsToProxy = [
107
107
*/
108
108
function defineResource ( definition ) {
109
109
var DS = this ;
110
+ var DSUtils = DS . utils ;
110
111
var definitions = DS . definitions ;
111
112
var IA = DS . errors . IA ;
112
113
113
- if ( DS . utils . isString ( definition ) ) {
114
+ if ( DSUtils . isString ( definition ) ) {
114
115
definition = definition . replace ( / \s / gi, '' ) ;
115
116
definition = {
116
117
name : definition
117
118
} ;
118
119
}
119
- if ( ! DS . utils . isObject ( definition ) ) {
120
+ if ( ! DSUtils . isObject ( definition ) ) {
120
121
throw new IA ( errorPrefix + 'definition: Must be an object!' ) ;
121
- } else if ( ! DS . utils . isString ( definition . name ) ) {
122
+ } else if ( ! DSUtils . isString ( definition . name ) ) {
122
123
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 ) ) {
124
125
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 ) ) {
126
127
throw new IA ( errorPrefix + 'definition.endpoint: Must be a string!' ) ;
127
128
} else if ( DS . store [ definition . name ] ) {
128
129
throw new DS . errors . R ( errorPrefix + definition . name + ' is already registered!' ) ;
@@ -131,20 +132,20 @@ function defineResource(definition) {
131
132
try {
132
133
// Inherit from global defaults
133
134
Resource . prototype = DS . defaults ;
134
- definitions [ definition . name ] = new Resource ( DS . utils , definition ) ;
135
+ definitions [ definition . name ] = new Resource ( DSUtils , definition ) ;
135
136
136
137
var def = definitions [ definition . name ] ;
137
138
138
139
// Setup nested parent configuration
139
140
if ( def . relations ) {
140
141
def . relationList = [ ] ;
141
142
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 ) ) {
145
146
relatedModels [ relationName ] = [ defs ] ;
146
147
}
147
- DS . utils . forEach ( relatedModels [ relationName ] , function ( d ) {
148
+ DSUtils . forEach ( relatedModels [ relationName ] , function ( d ) {
148
149
d . type = type ;
149
150
d . relation = relationName ;
150
151
d . name = def . name ;
@@ -154,17 +155,17 @@ function defineResource(definition) {
154
155
} ) ;
155
156
} ) ;
156
157
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 ) {
159
160
if ( relation . parent ) {
160
161
def . parent = modelName ;
161
162
def . parentKey = relation . localKey ;
162
163
}
163
164
} ) ;
164
165
} ) ;
165
166
}
166
- DS . utils . deepFreeze ( def . relations ) ;
167
- DS . utils . deepFreeze ( def . relationList ) ;
167
+ DSUtils . deepFreeze ( def . relations ) ;
168
+ DSUtils . deepFreeze ( def . relationList ) ;
168
169
}
169
170
170
171
def . getEndpoint = function ( attrs , options ) {
@@ -177,17 +178,17 @@ function defineResource(definition) {
177
178
options = options || { } ;
178
179
options . params = options . params || { } ;
179
180
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 ) ) {
181
182
item = DS . get ( this . name , attrs ) ;
182
183
}
183
- if ( DS . utils . isObject ( attrs ) && parentKey in attrs ) {
184
+ if ( DSUtils . isObject ( attrs ) && parentKey in attrs ) {
184
185
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 ) ;
186
187
} else if ( item && parentKey in item ) {
187
188
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 ) ;
189
190
} 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 ) ;
191
192
delete options . params [ parentKey ] ;
192
193
}
193
194
}
@@ -212,7 +213,7 @@ function defineResource(definition) {
212
213
deleteOnExpire : def . deleteOnExpire || 'none' ,
213
214
onExpire : function ( id ) {
214
215
var item = DS . eject ( def . name , id ) ;
215
- if ( DS . utils . isFunction ( def . onExpire ) ) {
216
+ if ( DSUtils . isFunction ( def . onExpire ) ) {
216
217
def . onExpire ( id , item ) ;
217
218
}
218
219
} ,
@@ -224,18 +225,18 @@ function defineResource(definition) {
224
225
} ) ;
225
226
226
227
// Create the wrapper class for the new resource
227
- def . class = DS . utils . pascalCase ( definition . name ) ;
228
+ def . class = DSUtils . pascalCase ( definition . name ) ;
228
229
eval ( 'function ' + def . class + '() {}' ) ;
229
230
def [ def . class ] = eval ( def . class ) ;
230
231
231
232
// Apply developer-defined methods
232
233
if ( def . methods ) {
233
- DS . utils . deepMixIn ( def [ def . class ] . prototype , def . methods ) ;
234
+ DSUtils . deepMixIn ( def [ def . class ] . prototype , def . methods ) ;
234
235
}
235
236
236
237
// Prepare for computed properties
237
238
if ( def . computed ) {
238
- DS . utils . forOwn ( def . computed , function ( fn , field ) {
239
+ DSUtils . forOwn ( def . computed , function ( fn , field ) {
239
240
if ( angular . isFunction ( fn ) ) {
240
241
def . computed [ field ] = [ fn ] ;
241
242
fn = def . computed [ field ] ;
@@ -257,7 +258,7 @@ function defineResource(definition) {
257
258
angular . forEach ( deps , function ( val , index ) {
258
259
deps [ index ] = val . trim ( ) ;
259
260
} ) ;
260
- fn . deps = DS . utils . filter ( deps , function ( dep ) {
261
+ fn . deps = DSUtils . filter ( deps , function ( dep ) {
261
262
return ! ! dep ;
262
263
} ) ;
263
264
} ) ;
@@ -309,6 +310,9 @@ function defineResource(definition) {
309
310
def . beforeDestroy = DS . $q . promisify ( def . beforeDestroy ) ;
310
311
def . afterDestroy = DS . $q . promisify ( def . afterDestroy ) ;
311
312
313
+ // Mix-in events
314
+ DSUtils . Events ( def ) ;
315
+
312
316
return def ;
313
317
} catch ( err ) {
314
318
DS . $log . error ( err ) ;
0 commit comments