Skip to content

Commit 4e213e3

Browse files
committed
Add sending unsaved ParseObjects to Clients (see parse-community/parse-server#7004)
1 parent 49df2f5 commit 4e213e3

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/__tests__/encode-test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const mockObject = function(className) {
1717
};
1818
mockObject.registerSubclass = function() {};
1919
mockObject.prototype = {
20+
id : 'objId123',
2021
_getServerData() {
2122
return this._serverData;
2223
},
@@ -38,6 +39,8 @@ mockObject.prototype = {
3839
__type: 'Object',
3940
className: this.className
4041
};
42+
seen = seen || [];
43+
offline = offline || false;
4144
for (const attr in this.attributes) {
4245
json[attr] = encode(this.attributes[attr], false, false, seen.concat(this), offline);
4346
}
@@ -167,6 +170,64 @@ describe('encode', () => {
167170
});
168171
});
169172

173+
it('encodes unsaved ParseObject', () => {
174+
const obj = new ParseObject('Item');
175+
obj.id = undefined;
176+
obj._serverData = {};
177+
obj.attributes = {
178+
str: 'string',
179+
date: new Date(Date.UTC(2015, 1, 1))
180+
};
181+
182+
expect(encode(obj)).toEqual({
183+
__type: 'Object',
184+
className: 'Item',
185+
str: 'string',
186+
date: {
187+
__type: 'Date',
188+
iso: '2015-02-01T00:00:00.000Z'
189+
}
190+
});
191+
192+
const subobj = new ParseObject('Subitem')
193+
subobj.id = undefined;
194+
subobj._serverData = {};
195+
subobj.attributes = {
196+
str: 'substring',
197+
};
198+
199+
obj.attributes = {
200+
item : subobj
201+
};
202+
203+
expect(encode(obj)).toEqual({
204+
__type: 'Object',
205+
className: 'Item',
206+
item: {
207+
__type: 'Object',
208+
className: 'Subitem',
209+
str:'substring'
210+
}
211+
});
212+
213+
obj.attributes = {
214+
items : [subobj, subobj]
215+
};
216+
expect(encode(obj)).toEqual({
217+
__type: 'Object',
218+
className: 'Item',
219+
items: [{
220+
__type: 'Object',
221+
className: 'Subitem',
222+
str:'substring'
223+
},{
224+
__type: 'Object',
225+
className: 'Subitem',
226+
str:'substring'
227+
}]
228+
});
229+
});
230+
170231
it('does not encode ParseObjects when they are disallowed', () => {
171232
const obj = new ParseObject('Item');
172233
expect(encode.bind(null, obj, true)).toThrow(

src/encode.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ function encode(value: mixed, disallowObjects: boolean, forcePointers: boolean,
3232
if (offline && value._getId().startsWith('local')) {
3333
return value.toOfflinePointer();
3434
}
35+
if (value.id === undefined){
36+
return value._toFullJSON();
37+
}
3538
return value.toPointer();
3639
}
3740
seen = seen.concat(seenEntry);

0 commit comments

Comments
 (0)