From 28acffd7dd6e6ababcf5f0c06f9facf7b136356e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Wed, 8 Apr 2015 11:52:58 +0200 Subject: [PATCH] Fix PersistedModel._defineChangeModel Correctly handle the case when the model is attached multiple times during the lifecycle, this happens because `loopback.createModel` always makes an attempt to auto-attach. --- lib/persisted-model.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/persisted-model.js b/lib/persisted-model.js index 53f8cc62..89b3b9e4 100644 --- a/lib/persisted-model.js +++ b/lib/persisted-model.js @@ -1423,10 +1423,15 @@ module.exports = function(registry) { if (this.dataSource) { attachRelatedModels(this); - } else { - this.once('dataSourceAttached', attachRelatedModels); } + // We have to attach related model whenever the datasource changes, + // this is a workaround for autoAttach called by loopback.createModel + var self = this; + this.on('dataSourceAttached', function() { + attachRelatedModels(self); + }); + return this.Change; function attachRelatedModels(self) {