Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: className in subclass constructor #1315

Merged
merged 4 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

## 3.1.0
[Full Changelog](https://github.com/parse-community/Parse-SDK-JS/compare/3.0.0...3.1.0)
Expand Down
17 changes: 17 additions & 0 deletions integration/test/ParseSubclassTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,21 @@ describe('Parse Object Subclasses', () => {
const wartortle = new Wartortle();
assert(wartortle.water);
});
it('registerSubclass with unknown className', async () => {
dblythy marked this conversation as resolved.
Show resolved Hide resolved
let outerClassName = '';
class TestObject extends Parse.Object {
constructor(className) {
super(className);
outerClassName = className;
}
}
Parse.Object.registerSubclass('TestObject', TestObject);
const o = new Parse.Object('TestObject');
await o.save();
const query = new Parse.Query('TestObject');
const first = await query.first();
expect(first instanceof TestObject).toBe(true);
expect(first.className).toBe('TestObject');
expect(outerClassName).toBe('TestObject');
});
});
12 changes: 3 additions & 9 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,7 @@ class ParseObject {
* @returns {Parse.Object}
*/
clone(): any {
const clone = new this.constructor();
if (!clone.className) {
clone.className = this.className;
}
const clone = new this.constructor(this.className);
let attributes = this.attributes;
if (typeof this.constructor.readOnlyAttributes === 'function') {
const readonly = this.constructor.readOnlyAttributes() || [];
Expand All @@ -959,10 +956,7 @@ class ParseObject {
* @returns {Parse.Object}
*/
newInstance(): any {
const clone = new this.constructor();
if (!clone.className) {
clone.className = this.className;
}
const clone = new this.constructor(this.className);
clone.id = this.id;
if (singleInstance) {
// Just return an object with the right id
Expand Down Expand Up @@ -1831,7 +1825,7 @@ class ParseObject {
throw new Error('Cannot create an object without a className');
}
const constructor = classMap[json.className];
const o = constructor ? new constructor() : new ParseObject(json.className);
const o = constructor ? new constructor(json.className) : new ParseObject(json.className);
const otherAttributes = {};
for (const attr in json) {
if (attr !== 'className' && attr !== '__type') {
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,22 @@ describe('ParseObject Subclasses', () => {
'You must register the subclass constructor. Did you attempt to register an instance of the subclass?'
);
});
it('can use on ParseObject subclass for multiple Parse.Object class names', () => {
dblythy marked this conversation as resolved.
Show resolved Hide resolved
class MyParseObjects extends ParseObject {
constructor(className) {
super(className);
}
}
ParseObject.registerSubclass('TestObject', MyParseObjects);
ParseObject.registerSubclass('TestObject1', MyParseObjects);
ParseObject.registerSubclass('TestObject2', MyParseObjects);
const obj = new MyParseObjects('TestObject');
expect(obj.className).toBe('TestObject');
const obj1 = new MyParseObjects('TestObject1');
expect(obj1.className).toBe('TestObject1');
const obj2 = new MyParseObjects('TestObject2');
expect(obj2.className).toBe('TestObject2');
});

it('can inflate subclasses from server JSON', () => {
const json = {
Expand Down