From 2a15dda6d245b9c81b05e28184c7fc728051cc18 Mon Sep 17 00:00:00 2001 From: Marc Smith Date: Thu, 20 Apr 2017 14:32:12 -0700 Subject: [PATCH] Allow the changing of objectId in beforeSave --- src/RestWrite.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/RestWrite.js b/src/RestWrite.js index 0cdc88bca9..35dcb9f10c 100644 --- a/src/RestWrite.js +++ b/src/RestWrite.js @@ -171,6 +171,26 @@ RestWrite.prototype.runBeforeTrigger = function() { return result; }, []); this.data = response.object; + + //Use Case: the beforeSave hook is trying to switch the changes to an existing object + //check if the objectId has been changed + if (this.query && this.query.objectId && response && response.object && response.object.id && response.object.id != this.query.objectId) { + //Check if the objectId on response.object exists by attempting a fetch + return response.object.fetch().then((object) => { + //the object exists + //Change the query to our new object + this.query.objectId = object.id; + //Change the original object to our "different" object + this.originalObject = object; + //We Don't delete the objectId + //delete this.data.objectId + //TODO: Does anything else need to be changed here? + },(error) => { + //the object Doesn't exist + //Delete the objectId + delete this.data.objectId + }); + } // We should delete the objectId for an update write if (this.query && this.query.objectId) { delete this.data.objectId