-
-
Notifications
You must be signed in to change notification settings - Fork 126
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
createViewModel doesn't work correctly with setters for computed values #214
Comments
I think it's a bug, would the following help? // https://github.com/mobxjs/mobx-utils/blob/1576286745c6229b34e1cf67353c3ccce1c6a843/src/create-view-model.ts#L56-L59
if (isComputedProp(model, key)) {
const modelComputedBox = _getAdministration(model, key) // Fixme: there is no clear api to get the derivation
const get = modelComputedBox.derivation.bind(this);
const set = modelComputedBox.setter
? modelComputedBox.setter.bind(this)
: undefined;
const localComputedBox = computed(get, { set });
this.localComputedValues.set(key, localComputedBox);
}
// https://github.com/mobxjs/mobx-utils/blob/1576286745c6229b34e1cf67353c3ccce1c6a843/src/create-view-model.ts#L72-L78
set: action((value: any) => {
if (isComputedProp(model, key)) {
this.localComputedValues.get(key).set(value);
} else if (value !== this.model[key as keyof T]) {
this.localValues.set(key, value)
} else {
this.localValues.delete(key)
}
}) |
@urugator |
@alex3683 interested in making a PR with this fix, including a test? |
@mweststrate Theoretically yes, but I guess I won't have the time to do this. I can try to do it, but I cannot promise any release date. Boss, wife and kids, all want me to do something else instead ;-) |
@alex3683 would you mind if i contribute to this one? |
@mweststrate btw note that the local computed ignores original computed's options (like |
@M-ZubairAhmed Go ahead. I don't want to block this. |
@urugator, yeah there are an unlimited amount of potential issues with prototypes, getters, enumerability and view models.... This So another solution would be to completely forbid the usage of getters / setters in view models.... |
Please don't. I use them a lot to transform string references into instances of other models and back. I think this is one of the greatest mobx features (including all the others ;-)) |
I fixed this issue independently in one of my projects, then saw this issue and now did go forward and merged my implementation with the proposed fix in this thread. See and please review the open PR. Additionally, if one is interested in a "deep" view model, one that is also able to transparently work with nested objects and arrays, then I could go forward and try to merge my private implementation into the mobx-utils, too... |
Fixed by #286 |
In my model I have several computed getters and accordingly setters, to derive the plain values from.
Simple example:
If I now create a view model from it using
createViewModel
, changes applied to thecolorCode
property are directly visible in the view. If I instead apply changes by using thecolorInstance
setter, it seems to have no effect. See here for the full, running example: https://codesandbox.io/s/reverent-dew-pb6mhWhen looking at the generated getters and setters in the code, the observation makes sense: computed values are always read from
localComputedValues
, while changes are always written tolocalValues
. Hence, in my examplecolorInstance
is written to thelocalValues
colorInstance
but read fromlocalComputedValues
, where it will never be up-to-date.Is this a bug or is it intended to work like that and I made a mistake somewhere?
If you need further details, feel free to ask.
The text was updated successfully, but these errors were encountered: