@@ -65,22 +65,21 @@ function Instance(opts) {
65
65
} ;
66
66
return checkNextValidation ( ) ;
67
67
} ;
68
+ var saveError = function ( cb , err ) {
69
+ emitEvent ( "save" , err , instance ) ;
70
+ Hook . trigger ( instance , opts . hooks . afterSave , false ) ;
71
+ if ( typeof cb == "function" ) {
72
+ cb ( err , instance ) ;
73
+ }
74
+ } ;
68
75
var saveInstance = function ( cb ) {
69
76
if ( ! opts . is_new && opts . changes . length === 0 ) {
70
77
return saveInstanceExtra ( cb ) ;
71
78
}
72
79
73
- var saveError = function ( err ) {
74
- emitEvent ( "save" , err , instance ) ;
75
- Hook . trigger ( instance , opts . hooks . afterSave , false ) ;
76
- if ( typeof cb == "function" ) {
77
- cb ( err , instance ) ;
78
- }
79
- } ;
80
-
81
80
Hook . wait ( instance , opts . hooks . beforeValidation , function ( err ) {
82
81
if ( err ) {
83
- return saveError ( err ) ;
82
+ return saveError ( cb , err ) ;
84
83
}
85
84
86
85
for ( var k in opts . properties ) {
@@ -103,19 +102,19 @@ function Instance(opts) {
103
102
if ( opts . is_new ) {
104
103
return Hook . wait ( instance , opts . hooks . beforeCreate , function ( err ) {
105
104
if ( err ) {
106
- return saveError ( err ) ;
105
+ return saveError ( cb , err ) ;
107
106
}
108
107
Hook . wait ( instance , opts . hooks . beforeSave , function ( err ) {
109
108
if ( err ) {
110
- return saveError ( err ) ;
109
+ return saveError ( cb , err ) ;
111
110
}
112
111
return saveInstanceNext ( cb ) ;
113
112
} ) ;
114
113
} ) ;
115
114
}
116
115
Hook . wait ( instance , opts . hooks . beforeSave , function ( err ) {
117
116
if ( err ) {
118
- return saveError ( err ) ;
117
+ return saveError ( cb , err ) ;
119
118
}
120
119
return saveInstanceNext ( cb ) ;
121
120
} ) ;
@@ -124,12 +123,7 @@ function Instance(opts) {
124
123
var saveInstanceNext = function ( cb ) {
125
124
handleValidations ( function ( err ) {
126
125
if ( err ) {
127
- emitEvent ( "save" , err , instance ) ;
128
- Hook . trigger ( instance , opts . hooks . afterSave , err === null ) ;
129
- if ( typeof cb == "function" ) {
130
- cb ( err , instance ) ;
131
- }
132
- return ;
126
+ return saveError ( cb , err ) ;
133
127
}
134
128
135
129
var data = { } ;
@@ -148,22 +142,27 @@ function Instance(opts) {
148
142
149
143
if ( opts . is_new ) {
150
144
opts . driver . insert ( opts . table , data , opts . keys , function ( save_err , info ) {
151
- if ( ! save_err ) {
152
- opts . changes . length = 0 ;
153
- for ( var i = 0 ; i < opts . keys . length ; i ++ ) {
154
- opts . data [ opts . keys [ i ] ] = info [ opts . keys [ i ] ] ;
155
- }
156
- opts . is_new = false ;
145
+ if ( save_err ) {
146
+ return saveError ( cb , save_err ) ;
157
147
}
158
- emitEvent ( "save" , save_err , instance ) ;
159
- Hook . trigger ( instance , opts . hooks . afterCreate , ! save_err ) ;
160
- Hook . trigger ( instance , opts . hooks . afterSave , ! save_err ) ;
161
- if ( typeof cb == "function" ) {
162
- if ( save_err ) {
163
- return cb ( save_err , instance ) ;
164
- }
165
- return saveInstanceExtra ( cb ) ;
148
+
149
+ opts . changes . length = 0 ;
150
+ for ( var i = 0 ; i < opts . keys . length ; i ++ ) {
151
+ opts . data [ opts . keys [ i ] ] = info [ opts . keys [ i ] ] ;
166
152
}
153
+ opts . is_new = false ;
154
+
155
+ saveAssociations ( function ( err ) {
156
+ emitEvent ( "save" , err , instance ) ;
157
+ Hook . trigger ( instance , opts . hooks . afterCreate , ! err ) ;
158
+ Hook . trigger ( instance , opts . hooks . afterSave , ! err ) ;
159
+ if ( typeof cb == "function" ) {
160
+ if ( err ) {
161
+ return cb ( err , instance ) ;
162
+ }
163
+ return saveInstanceExtra ( cb ) ;
164
+ }
165
+ } ) ;
167
166
} ) ;
168
167
} else {
169
168
var changes = { } , conditions = { } ;
@@ -174,21 +173,73 @@ function Instance(opts) {
174
173
conditions [ opts . keys [ i ] ] = data [ opts . keys [ i ] ] ;
175
174
}
176
175
opts . driver . update ( opts . table , changes , conditions , function ( save_err ) {
177
- if ( ! save_err ) {
178
- opts . changes . length = 0 ;
176
+ if ( save_err ) {
177
+ return saveError ( cb , save_err ) ;
179
178
}
180
- emitEvent ( "save" , save_err , instance ) ;
181
- Hook . trigger ( instance , opts . hooks . afterSave , ! save_err ) ;
182
- if ( typeof cb == "function" ) {
183
- if ( save_err ) {
184
- return cb ( save_err , instance ) ;
179
+
180
+ opts . changes . length = 0 ;
181
+
182
+ saveAssociations ( function ( err ) {
183
+ emitEvent ( "save" , err , instance ) ;
184
+ Hook . trigger ( instance , opts . hooks . afterSave , ! err ) ;
185
+ if ( typeof cb == "function" ) {
186
+ if ( err ) {
187
+ return cb ( err , instance ) ;
188
+ }
189
+ return saveInstanceExtra ( cb ) ;
185
190
}
186
- return saveInstanceExtra ( cb ) ;
187
- }
191
+ } ) ;
188
192
} ) ;
189
193
}
190
194
} ) ;
191
195
} ;
196
+ var saveAssociations = function ( cb ) {
197
+ var pending = 0 , errored = false , i , j ;
198
+
199
+ for ( i = 0 ; i < opts . one_associations . length ; i ++ ) {
200
+ if ( ! instance . hasOwnProperty ( opts . one_associations [ i ] . name ) ) continue ;
201
+
202
+ if ( instance [ opts . one_associations [ i ] . name ] . isInstance ) {
203
+ pending += 1 ;
204
+
205
+ instance [ opts . one_associations [ i ] . setAccessor ] ( instance [ opts . one_associations [ i ] . name ] , function ( err ) {
206
+ if ( err ) {
207
+ if ( errored ) return ;
208
+
209
+ errored = true ;
210
+ return cb ( err ) ;
211
+ }
212
+
213
+ if ( -- pending === 0 ) {
214
+ return cb ( ) ;
215
+ }
216
+ } ) ;
217
+ }
218
+ }
219
+
220
+ for ( i = 0 ; i < opts . many_associations . length ; i ++ ) {
221
+ if ( ! instance . hasOwnProperty ( opts . many_associations [ i ] . name ) ) continue ;
222
+
223
+ pending += 1 ;
224
+
225
+ instance [ opts . many_associations [ i ] . setAccessor ] ( instance [ opts . many_associations [ i ] . name ] , function ( err ) {
226
+ if ( err ) {
227
+ if ( errored ) return ;
228
+
229
+ errored = true ;
230
+ return cb ( err ) ;
231
+ }
232
+
233
+ if ( -- pending === 0 ) {
234
+ return cb ( ) ;
235
+ }
236
+ } ) ;
237
+ }
238
+
239
+ if ( pending === 0 ) {
240
+ return cb ( ) ;
241
+ }
242
+ } ;
192
243
var saveInstanceExtra = function ( cb ) {
193
244
if ( opts . extrachanges . length === 0 ) {
194
245
if ( cb ) return cb ( null , instance ) ;
@@ -422,6 +473,18 @@ function Instance(opts) {
422
473
break ;
423
474
}
424
475
}
476
+ for ( i = 0 ; i < opts . one_associations . length ; i ++ ) {
477
+ if ( opts . data . hasOwnProperty ( opts . one_associations [ i ] . name ) ) {
478
+ instance [ opts . one_associations [ i ] . name ] = opts . data [ opts . one_associations [ i ] . name ] ;
479
+ delete opts . data [ opts . one_associations [ i ] . name ] ;
480
+ }
481
+ }
482
+ for ( i = 0 ; i < opts . many_associations . length ; i ++ ) {
483
+ if ( opts . data . hasOwnProperty ( opts . many_associations [ i ] . name ) ) {
484
+ instance [ opts . many_associations [ i ] . name ] = opts . data [ opts . many_associations [ i ] . name ] ;
485
+ delete opts . data [ opts . many_associations [ i ] . name ] ;
486
+ }
487
+ }
425
488
426
489
Hook . trigger ( instance , opts . hooks . afterLoad ) ;
427
490
0 commit comments