| 294 | } |
| 295 | |
| 296 | function autoFetchInstance(Instance, association, opts, cb) { |
| 297 | if (!Instance.saved()) { |
| 298 | return cb(); |
| 299 | } |
| 300 | |
| 301 | if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit === "undefined") { |
| 302 | opts.autoFetchLimit = association.autoFetchLimit; |
| 303 | } |
| 304 | |
| 305 | if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch)) { |
| 306 | return cb(); |
| 307 | } |
| 308 | |
| 309 | // When we have a new non persisted instance for which the association field (eg owner_id) |
| 310 | // is set, we don't want to auto fetch anything, since `new Model(owner_id: 12)` takes no |
| 311 | // callback, and hence this lookup would complete at an arbitrary point in the future. |
| 312 | // The associated entity should probably be fetched when the instance is persisted. |
| 313 | if (Instance.isPersisted()) { |
| 314 | Instance[association.getAccessor]({ autoFetchLimit: opts.autoFetchLimit - 1 }, cb); |
| 315 | } else { |
| 316 | return cb(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | function ucfirst(text) { |
| 321 | return text[0].toUpperCase() + text.substr(1); |