|
1 | 1 | var _ = require("lodash"); |
| 2 | +var async = require("async"); |
2 | 3 | var util = require("../Utilities"); |
3 | 4 | var ORMError = require("../Error"); |
4 | 5 | var Accessors = { "get": "get", "set": "set", "has": "has", "del": "remove" }; |
@@ -214,52 +215,39 @@ function extendInstance(Model, Instance, Driver, association) { |
214 | 215 | writable: true |
215 | 216 | }); |
216 | 217 | Object.defineProperty(Instance, association.setAccessor, { |
217 | | - value: function (OtherInstance, cb) { |
| 218 | + value: function (OtherInstance, next) { |
218 | 219 | if (association.reversed) { |
219 | 220 | Instance.save(function (err) { |
220 | 221 | if (err) { |
221 | | - return cb(err); |
| 222 | + return next(err); |
222 | 223 | } |
223 | 224 |
|
224 | 225 | if (!Array.isArray(OtherInstance)) { |
225 | 226 | util.populateConditions(Model, Object.keys(association.field), Instance, OtherInstance, true); |
226 | 227 |
|
227 | | - return OtherInstance.save({}, { saveAssociations: false }, cb); |
| 228 | + return OtherInstance.save({}, { saveAssociations: false }, next); |
228 | 229 | } |
229 | 230 |
|
230 | | - var associations = _.clone(OtherInstance); |
231 | | - |
232 | | - var saveNext = function () { |
233 | | - if (!associations.length) { |
234 | | - return cb(); |
235 | | - } |
236 | | - |
237 | | - var other = associations.pop(); |
| 231 | + var saveAssociation = function (otherInstance, cb) { |
| 232 | + util.populateConditions(Model, Object.keys(association.field), Instance, otherInstance, true); |
238 | 233 |
|
239 | | - util.populateConditions(Model, Object.keys(association.field), Instance, other, true); |
240 | | - |
241 | | - other.save({}, { saveAssociations: false }, function (err) { |
242 | | - if (err) { |
243 | | - return cb(err); |
244 | | - } |
245 | | - |
246 | | - saveNext(); |
247 | | - }); |
| 234 | + otherInstance.save({}, { saveAssociations: false }, cb); |
248 | 235 | }; |
249 | 236 |
|
250 | | - return saveNext(); |
| 237 | + var associations = _.clone(OtherInstance); |
| 238 | + async.eachSeries(associations, saveAssociation, next); |
251 | 239 | }); |
252 | 240 | } else { |
253 | 241 | OtherInstance.save({}, { saveAssociations: false }, function (err) { |
254 | 242 | if (err) { |
255 | | - return cb(err); |
| 243 | + return next(err); |
256 | 244 | } |
257 | 245 |
|
258 | 246 | Instance[association.name] = OtherInstance; |
259 | 247 |
|
260 | 248 | util.populateConditions(association.model, Object.keys(association.field), OtherInstance, Instance); |
261 | 249 |
|
262 | | - return Instance.save({}, { saveAssociations: false }, cb); |
| 250 | + return Instance.save({}, { saveAssociations: false }, next); |
263 | 251 | }); |
264 | 252 | } |
265 | 253 |
|
|
0 commit comments