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

Already on GitHub? Sign in to your account

Statics and methods are all overwritten #8

Open
jamesgpearce opened this Issue Apr 5, 2013 · 7 comments

Comments

Projects
None yet
4 participants

If the parent model has

schema.statics = {a: '1', b: '2'};

and the child has

schema.statics = {b: '1', c: '2'};

Then the resulting statics should be {a: '1', b: '1', c:'2'} (i.e. methods should be overridden on a case-by-case basis). But in fact none of the parent's values are inherited.

Same goes for schema.methods

The current way around this is for the child to have

schema.statics.b = '1'; schema.statics.c = '2';

...which is frustrating & esoteric syntax.

Thanks!

@jamesgpearce, can't see a problem here, because the 'inherited' properties .statics and .methods are just cloned to child schemas, that's all.

Having the 'inheritance' working the way you mentioned is not essential for JS, so you have to deal with the fact that you have different objects, and, if you don't like the syntax, you can always use methods like require('lodash').assign(child_schema, {b:'1', c:'2'})

@sieira sieira added the enhancement label Nov 11, 2015

@sieira sieira self-assigned this Nov 11, 2015

Collaborator

sieira commented Feb 14, 2016

I'd say jamesgpearce is right... I'm uploading a PR just to illustrate it

visionscaper commented Apr 15, 2016 edited

Would it be possible to have a "super" object to get access to the parent method definitions?
Then I would be able to do the following:

MySchema.methods.myMethod = function() {
    var result = this.super.myMethod();
    // ...
    // Further process or extend results here
    // ...

    return result;
}
Collaborator

sieira commented Apr 16, 2016

Sounds good. Unfortunately, I do not have the time to maintain this, please feel free to send a PR!

Hi @sieira,

Because I need to start my older node.js as node --harmony_proxies --harmony to use this module, and the discriminator method is available for a while already, I created (just now) my own module that I open sourced here.

Super works a bit different in this library than proposed earlier :

MySchema.methods.myMethod = function() {
    var result = this.super('myMethod')();
    // ...
    // Further process or extend results here
    // ...

    return result;
};

Kind regards,

Freddy

Collaborator

sieira commented Jun 20, 2016

@visionscaper Looks very good!

Thanks, @sieira! It has been working well for me and looking at the stats on npmjs.com it is frequently downloaded by others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment