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

Conversation

dblythy
Copy link
Member

@dblythy dblythy commented Mar 8, 2021

Hi,

Let's say you want to create a global function getName on all your Parse.Objects. Let's say you have 4 different classes. Here's how you'd have to do that now:

class ClassOne extends Parse.Object {
  constructor() {
    super('ClassOne');
  }
  getDetails() {
    return `${this.get('title')} ${this.get('description')}`;
  }
}
class ClassTwo extends Parse.Object {
  constructor() {
    super('ClassTwo');
  }
  getDetails() {
    return `${this.get('title')} ${this.get('description')}`;
  }
}
class ClassThree extends Parse.Object {
  constructor() {
    super('ClassThree');
  }
  getDetails() {
    return `${this.get('title')} ${this.get('description')}`;
  }
}
Parse.Object.registerSubclass('ClassOne', ClassOne);
Parse.Object.registerSubclass('ClassTwo', ClassTwo);
Parse.Object.registerSubclass('ClassThree', ClassThree);

Now, obviously this doesn't apply to all use cases, but this allows a registeredSubclass to have multiple class names, meaning that you can create one overhead class for multiple Parse Objects.

class ParseHelper extends Parse.Object {
  constructor(className) {
    super(className);
  }
  getDetails() {
    return `${this.get('title')} ${this.get('description')}`;
  }
}
const classNames = ['ClassOne', 'ClassTwo', 'ClassThree'];
for (const className of classNames) {
  Parse.Object.registerSubclass(className, ParseHelper);
}

@codecov
Copy link

codecov bot commented Mar 8, 2021

Codecov Report

Merging #1315 (cbae1c4) into master (29fc171) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1315   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           60        60           
  Lines         5851      5847    -4     
  Branches      1314      1312    -2     
=========================================
- Hits          5851      5847    -4     
Impacted Files Coverage Δ
src/ParseObject.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 29fc171...cbae1c4. Read the comment docs.

@dplewis
Copy link
Member

dplewis commented Mar 8, 2021

Can you write a failing test? I kinda know what you are trying to do.

@dblythy
Copy link
Member Author

dblythy commented Mar 8, 2021

Can you write a failing test? I kinda know what you are trying to do.

Yes, the main problem is that subclassing doesn't work without super(ClassName), and ClassName has to be, the specific object's class name.

it('can use on ParseObject subclass for multiple Parse.Object class names', () => {
  class MyParseObjects extends ParseObject {
    constructor() {
      super();
    }
  
    doSomething() {
      return 5;
    }
  
    static readOnlyAttributes() {
      return ['readonly', 'static', 'frozen'];
    }
  }
  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');
});

Copy link
Member

@dplewis dplewis left a comment

Choose a reason for hiding this comment

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

Just a quick nit. Nice catch!

src/__tests__/ParseObject-test.js Show resolved Hide resolved
integration/test/ParseSubclassTest.js Show resolved Hide resolved
@dplewis dplewis merged commit 724485d into parse-community:master Mar 9, 2021
@dblythy
Copy link
Member Author

dblythy commented Mar 9, 2021

Thanks @dplewis! Will be very helpful for me on Vue projects.

Note that this will have implications for #924

@dplewis
Copy link
Member

dplewis commented Mar 9, 2021

I’m surprise nobody caught this. Should look into #924

@dblythy dblythy deleted the classNameConstructor branch March 9, 2021 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants