Skip to content

Commit 724485d

Browse files
authored
new: className in subclass constructor (#1315)
* new: className in constructor * Update CHANGELOG.md * update tests * add spaces
1 parent 29fc171 commit 724485d

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### master
44
[Full Changelog](https://github.com/parse-community/Parse-SDK-JS/compare/3.1.0...master)
5+
- Add className argument to Parse Object subclass constructor ([#1315](https://github.com/parse-community/Parse-SDK-JS/pull/1315))
56

67
## 3.1.0
78
[Full Changelog](https://github.com/parse-community/Parse-SDK-JS/compare/3.0.0...3.1.0)

integration/test/ParseSubclassTest.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,22 @@ describe('Parse Object Subclasses', () => {
193193
const wartortle = new Wartortle();
194194
assert(wartortle.water);
195195
});
196+
197+
it('registerSubclass with unknown className', async () => {
198+
let outerClassName = '';
199+
class TestObject extends Parse.Object {
200+
constructor(className) {
201+
super(className);
202+
outerClassName = className;
203+
}
204+
}
205+
Parse.Object.registerSubclass('TestObject', TestObject);
206+
const o = new Parse.Object('TestObject');
207+
await o.save();
208+
const query = new Parse.Query('TestObject');
209+
const first = await query.first();
210+
expect(first instanceof TestObject).toBe(true);
211+
expect(first.className).toBe('TestObject');
212+
expect(outerClassName).toBe('TestObject');
213+
});
196214
});

src/ParseObject.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,7 @@ class ParseObject {
930930
* @returns {Parse.Object}
931931
*/
932932
clone(): any {
933-
const clone = new this.constructor();
934-
if (!clone.className) {
935-
clone.className = this.className;
936-
}
933+
const clone = new this.constructor(this.className);
937934
let attributes = this.attributes;
938935
if (typeof this.constructor.readOnlyAttributes === 'function') {
939936
const readonly = this.constructor.readOnlyAttributes() || [];
@@ -959,10 +956,7 @@ class ParseObject {
959956
* @returns {Parse.Object}
960957
*/
961958
newInstance(): any {
962-
const clone = new this.constructor();
963-
if (!clone.className) {
964-
clone.className = this.className;
965-
}
959+
const clone = new this.constructor(this.className);
966960
clone.id = this.id;
967961
if (singleInstance) {
968962
// Just return an object with the right id
@@ -1831,7 +1825,7 @@ class ParseObject {
18311825
throw new Error('Cannot create an object without a className');
18321826
}
18331827
const constructor = classMap[json.className];
1834-
const o = constructor ? new constructor() : new ParseObject(json.className);
1828+
const o = constructor ? new constructor(json.className) : new ParseObject(json.className);
18351829
const otherAttributes = {};
18361830
for (const attr in json) {
18371831
if (attr !== 'className' && attr !== '__type') {

src/__tests__/ParseObject-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,23 @@ describe('ParseObject Subclasses', () => {
34263426
);
34273427
});
34283428

3429+
it('can use on ParseObject subclass for multiple Parse.Object class names', () => {
3430+
class MyParseObjects extends ParseObject {
3431+
constructor(className) {
3432+
super(className);
3433+
}
3434+
}
3435+
ParseObject.registerSubclass('TestObject', MyParseObjects);
3436+
ParseObject.registerSubclass('TestObject1', MyParseObjects);
3437+
ParseObject.registerSubclass('TestObject2', MyParseObjects);
3438+
const obj = new MyParseObjects('TestObject');
3439+
expect(obj.className).toBe('TestObject');
3440+
const obj1 = new MyParseObjects('TestObject1');
3441+
expect(obj1.className).toBe('TestObject1');
3442+
const obj2 = new MyParseObjects('TestObject2');
3443+
expect(obj2.className).toBe('TestObject2');
3444+
});
3445+
34293446
it('can inflate subclasses from server JSON', () => {
34303447
const json = {
34313448
className: 'MyObject',

0 commit comments

Comments
 (0)