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

Introduce support for setters for computed properties #463

Closed
mweststrate opened this issue Aug 7, 2016 · 7 comments
Closed

Introduce support for setters for computed properties #463

mweststrate opened this issue Aug 7, 2016 · 7 comments

Comments

@mweststrate
Copy link
Member

No description provided.

@jeffijoe
Copy link
Contributor

jeffijoe commented Aug 8, 2016

How would this work? Isn't the point of computed that the data is always derived?

@mweststrate
Copy link
Member Author

note to self: use actions. @jeffijoe yes, it can't be used to change the computed, but it could be used to change the observable behind the computed, e.g.:

class Distance {

@observable distance = 1

@computed get distanceInMiles() {
  return this.distance * 1.6
}
set distanceInMiles(newValue) {
   this.distance = newValue / 1.6
}

}

@jeffijoe
Copy link
Contributor

@mweststrate that's what I imagine as well - will that example you posted not work today?

@andykog
Copy link
Member

andykog commented Aug 10, 2016

That could nicely fit my proposal #421

extendObservable(this, {
  distance: 1,
  distanceInMiles: computed(
    () => this.count * 1.6, // get
    miles => this.distance = miles / 1.6, // set
  )
});

// or:

extendObservable(this, {
  distance: 1,
  distanceInMiles: computed({
    get: () => this.count * 1.6,
    set: miles => this.distance = miles / 1.6,
  })
});

@mweststrate
Copy link
Member Author

Currently implementing proposal 1 of @andykog 's example. The ES6 variant
will be

class something {
  @observable x = 1
  @computed get y() { 
    return this. x * 2
  }
  set(v) {
    this.x = v / 2
   }
}

See #421 I thin using computed with (extend)Observable should be the idiomatic and promoted approach, but probably it is too breaking to already give a deprecation message for the old approach, maybe in the next minor and then remove it in 3.0

@andykog
Copy link
Member

andykog commented Aug 30, 2016

@mweststrate you mean set y(v) { this.x = v / 2 }, right?

@mweststrate
Copy link
Member Author

Closing, this was released as 2.5.0

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

No branches or pull requests

3 participants