-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
class overrides problem #600
Comments
Note that overriding properties is in principle not supported. If you want to have a inherited |
Thanks for your comments, but I do not know how to write the helper function, define a new toJson method but inherited baseProp properties in ChildClass with extendObservable (case 1). could you give me more tips? thanks! btw, in large application, how do you handle class extends problem? I do like to have a base store, and inherit it to get another store. |
class BaseClass {
constructor(baseProp = 1) {
extendObservable(this, {
baseProp: baseProp,
// observable, not overridable
get toJson() {
return this.toJsonHelper;
}
});
}
// not observable, overridable
get toJsonHelper() {
return {baseProp: this.baseProp};
}
}
class ChildClass extends BaseClass {
constructor(prop=2) {
super(prop);
extendObservable(this, {
color: 'black',
});
}
// override helper
get toJsonHelper() {
return {
baseProp: this.baseProp,
color: this.color
};
}
} Btw I don't think it's mentioned anywhere in doc... |
@urugator Thanks! It works. Anyway, I suppose the |
I think this issue is not common enough to justify further investigation. Overriding fields is a bad idea in most programming languages, and so it is in this case :) |
hello 2017 ! i'm hitting similar issue 3 years after. i know mobx5 is discontinued, but i'm only looking for clues on how to solve it (not produce a clean fix). Following code works with babel6/mobx4, but breaks in babel7/mobx5: class Model {
static Schema = {}
constructor(data) {
const schema = this.constructor.Schema
const observables = Object.fromEntries(
Object.keys(schema).map(key => ([key, this[key]]))
)
extendObservable(this, observables)
this.update(data)
}
@action.bound update(data) {
set(this, data)
}
}
class User extends Model {
static Schema = {
userId: joi.string()
}
userId: string
}
const user = new User({ userId: 'foo' })
console.log(user.userId) // undefined If I comment the prop definition of |
@y-nk Please, rather than commenting on super old and closed issues, open a new discussion so other people can see it. Obviously, your issue is not the same, otherwise, you would have found your answer above. Also note that MobX 4/5 is not supported anymore, so consider upgrading to V6 first. |
case 1 JS Bin
mobx@2.4.3
the result is:
but I do like to see this result:
case 2 JS Bin
same code, but with mobx@2.6.0, the result is
case 3 JS Bin
use
@observable
and mobx@2.6.0it works! why? I really sucked about no decorators way....but...
The text was updated successfully, but these errors were encountered: