Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ type SaveParams = {
};

type SaveOptions = FullOptions & {
cascadeSave?: boolean
cascadeSave?: boolean;
context?: AttributeMap;
}

// Mapping of class names to constructors, so we can populate objects from the
Expand Down Expand Up @@ -1179,6 +1180,7 @@ class ParseObject {
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* <li>cascadeSave: If `false`, nested objects will not be saved (default is `true`).
* <li>context: A dictionary that is accessible in Cloud Code `beforeSave` and `afterSave` triggers.
* </ul>
* </li>
* </ul>
Expand All @@ -1192,6 +1194,7 @@ class ParseObject {
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* <li>cascadeSave: If `false`, nested objects will not be saved (default is `true`).
* <li>context: A dictionary that is accessible in Cloud Code `beforeSave` and `afterSave` triggers.
* </ul>
*
* @return {Promise} A promise that is fulfilled when the save
Expand Down Expand Up @@ -1251,6 +1254,9 @@ class ParseObject {
if (options.hasOwnProperty('installationId') && typeof options.installationId === 'string') {
saveOptions.installationId = options.installationId;
}
if (options.hasOwnProperty('context') && typeof options.context === 'object') {
saveOptions.context = options.context;
}
const controller = CoreManager.getObjectController();
const unsaved = options.cascadeSave !== false ? unsavedChildren(this) : null;
return controller.save(unsaved, saveOptions).then(() => {
Expand Down
8 changes: 8 additions & 0 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type RequestOptions = {
batchSize?: number;
include?: any;
progress?: any;
context?: any;
};

export type FullOptions = {
Expand Down Expand Up @@ -222,6 +223,13 @@ const RESTController = {
}
}

// Add context
const context = options.context;
if (context !== undefined) {
payload._context = context;
delete options.context;
}

if (method !== 'POST') {
payload._method = method;
method = 'POST';
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,26 @@ describe('ParseObject', () => {
});
});

it('accepts context on save', async () => {
// Mock XHR
CoreManager.getRESTController()._setXHR(
mockXHR([{
status: 200,
response: { objectId: 'newattributes' }
}])
);
// Spy on REST controller
const controller = CoreManager.getRESTController();
jest.spyOn(controller, 'ajax');
// Save object
const context = {a: "a"};
const obj = new ParseObject('Item');
await obj.save(null, {context});
// Validate
const jsonBody = JSON.parse(controller.ajax.mock.calls[0][2]);
expect(jsonBody._context).toEqual(context);
});

it('interpolates delete operations', (done) => {
CoreManager.getRESTController()._setXHR(
mockXHR([{
Expand Down