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

static collectionName improvement #16

Closed
resistdesign opened this issue Dec 2, 2015 · 2 comments
Closed

static collectionName improvement #16

resistdesign opened this issue Dec 2, 2015 · 2 comments

Comments

@resistdesign
Copy link

You asked how to do this better here: https://github.com/scottwrobinson/camo/blob/master/lib/document.js#L41

Just use a static property static collectionName = 'people'; instead of using super('people').

Then your Document class can do this.constructor.collectionName to access it.

http://jsfiddle.net/resistdesign/wgsh2sex/

@scottwrobinson
Copy link
Owner

Thanks for the suggestion, Ryan! Unfortunately static properties aren't available in Node right now, and as much as I'd like to use Babel I don't think it would be reasonable to require the user to do so also.

Instead I've been thinking we should just do something like this:

'use strict';

class Document {
  constructor() {

  }

  static collectionName() {
    return this.name.toLowerCase();
  }
}

class Person extends Document {
  constructor() {
    super();
  }

  static collectionName() {
    return 'people';
  }
}

class Bill extends Person {
  constructor() {
    super();
  }
}

console.log('Document:', Document.collectionName());  // Prints 'document'
console.log('Person:', Person.collectionName());      // Prints 'people'
console.log('Bill:', Bill.collectionName());          // Prints 'people'

Requiring the user to add the collectionName() function isn't as good as using a property, but it's better than what we're doing now. Also, if collectionName() isn't provided we just use the class name by default (and optionally with Pluralize to make it look nicer).

Thoughts?

Thanks for the suggestion!

@resistdesign
Copy link
Author

Honestly, short of static props, that's a really nice solution and I'd be happy to use it :)

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

No branches or pull requests

2 participants