Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion spec/rest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ describe('rest create', () => {

it('handles object and subdocument', done => {
const obj = { subdoc: {foo: 'bar', wu: 'tan'} };

Parse.Cloud.beforeSave('MyClass', function(req, res) {
// this beforeSave trigger should do nothing but can mess with the object
res.success();
});

rest.create(config, auth.nobody(config), 'MyClass', obj)
.then(() => database.adapter.find('MyClass', { fields: {} }, {}, {}))
.then(results => {
Expand All @@ -64,7 +70,7 @@ describe('rest create', () => {
expect(mob.subdoc.wu).toBe('tan');
expect(typeof mob.objectId).toEqual('string');
const obj = { 'subdoc.wu': 'clan' };
return rest.update(config, auth.nobody(config), 'MyClass', { objectId: mob.objectId }, obj)
return rest.update(config, auth.nobody(config), 'MyClass', { objectId: mob.objectId }, obj);
})
.then(() => database.adapter.find('MyClass', { fields: {} }, {}, {}))
.then(results => {
Expand Down
29 changes: 25 additions & 4 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ RestWrite.prototype.runBeforeTrigger = function() {
}

let originalObject = null;
const updatedObject = triggers.inflate(extraData, this.originalData);
const updatedObject = this.buildUpdatedObject(extraData);
if (this.query && this.query.objectId) {
// This is an update for existing object.
originalObject = triggers.inflate(extraData, this.originalData);
}
updatedObject.set(this.sanitizedData());

return Promise.resolve().then(() => {
return triggers.maybeRunTrigger(triggers.Types.beforeSave, this.auth, updatedObject, originalObject, this.config);
Expand Down Expand Up @@ -1068,8 +1067,7 @@ RestWrite.prototype.runAfterTrigger = function() {

// Build the inflated object, different from beforeSave, originalData is not empty
// since developers can change data in the beforeSave.
const updatedObject = triggers.inflate(extraData, this.originalData);
updatedObject.set(this.sanitizedData());
const updatedObject = this.buildUpdatedObject(extraData);
updatedObject._handleSaveResponse(this.response.response, this.response.status || 200);

// Notifiy LiveQueryServer if possible
Expand Down Expand Up @@ -1104,6 +1102,29 @@ RestWrite.prototype.sanitizedData = function() {
return Parse._decode(undefined, data);
}

// Returns an updated copy of the object
RestWrite.prototype.buildUpdatedObject = function (extraData) {
var updatedObject = triggers.inflate(extraData, this.originalData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use const/let instead of var

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, done

Copy link
Contributor

@natanrolnik natanrolnik Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You apparently missed this one, @IlyaDiallo , no?
It seems you replaced the other ones but not this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that the style requirement of let instead of var was only for declarations inside blocks, not at the beginning of a function. Is that wrong ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand/can answer. @flovilmart, what do you say?

Copy link
Contributor Author

@IlyaDiallo IlyaDiallo Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean than this does not conform to the guideline because the var is visible outside the if block:
function f() { if(test) { var v; } }
... but this is (maybe) OK because var and let have the same scope:
function f() { var v; }

BTW is there a written styling guide for the project ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS here it's in fact a const variable , so I've replaced var with const anyway, but the style question still stands.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use const for immutable variable (never reassigned) and let in place of vars as they behave more appropriately.

Object.keys(this.data).reduce(function (data, key) {
if (key.indexOf(".") > 0) {
// subdocument key with dot notation ('x.y':v => 'x':{'y':v})
var splittedKey = key.split(".");
var parentProp = splittedKey[0];
var parentVal = updatedObject.get(parentProp);
if(typeof parentVal !== 'object') {
parentVal = {};
}
parentVal[splittedKey[1]] = data[key];
updatedObject.set(parentProp, parentVal);
delete data[key];
}
return data;
}, deepcopy(this.data));

updatedObject.set(this.sanitizedData());
return updatedObject;
};

RestWrite.prototype.cleanUserAuthData = function() {
if (this.response && this.response.response && this.className === '_User') {
const user = this.response.response;
Expand Down