I tried to migrate form Backbone states to mobx, but I cant find easy solutions in some situations.
For example in Backbone when attributes changed model keeps previous value of it ( modell.previous("sum") ). Is it possible to get it in mobx without using Intercept or observe?
Small example:
class SomeStore{
@observable sum = 100;
@computed get diff() {
//ideally I'd like to compute diff between old and new sum
//like: return this.sum - this.sum.old
return this.sum
}
constructor(){
setTimeout(()=>{
this.sum = 200;
}, 1000);
autorun(()=>{
console.log([this.diff]);
});
}
}
Thanks a lot!
You'd have to store the previous value or use something like observe.
On Nov 18, 2016 5:25 AM, "alexander812" [email protected] wrote:
I tried to migrate form Backbone states to mobx, but I cant find easy
solutions in some situations.
For example in Backbone when attributes changed model keeps previous value
of it ( modell.previous("sum") ). Is it possible to get it in mobx with out
using Intercept or observe?Small example:
`
class SomeStore{
@observable https://github.com/observable sum = 100;@computed get diff() { //ideally I'd like to computed diff between old and new sum //like: return this.sum - this.sum.old return this.sum } constructor(){ setTimeout(()=>{ this.sum = 200; }, 1000); autorun(()=>{ console.log([this.diff]); }); }}
`
Thanks a lot!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx/issues/664, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAIrctvA0oPyzYIlO0ZBcYzT8ybsJvX3ks5q_YsngaJpZM4K2Ycv
.
If I got right the only way is to like this:
class SomeStore{
@observable sum = 100;
@observable sumPrev = 100;
@computed get diff() {
return this.sum - this.sumPrev;
}
constructor(){
observe(this, "sum", (newValue, oldValue) => {
this.sumPrev = oldValue;
});
}
}
That or update the previous value when you set sum.
On Nov 18, 2016 8:12 AM, "alexander812" [email protected] wrote:
If I got right the only way is to like this:
class SomeStore{
@observable sum = 100;
@observable sumPrev = 100;
@computed get diff() {
return this.sum - this.sumPrev;
}
constructor(){
observe(this, "sum", (newValue, oldValue) => {
this.sumPrev = oldValue;
});
}
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx/issues/664#issuecomment-261540485, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAIrcqVAjmqhMZ6isj6qVsdFF6P3jOhaks5q_bJXgaJpZM4K2Ycv
.
Or another alternative:
class SomeStore{
@observable sum = 100;
/* not observable */ sumPrev = 100;
@computed get diff() {
const res = this.sum - this.sumPrev;
this.sumPrev = res
return res
}
}
Thanks a lot! Good solution!
Done
If I got right the only way is to like this:
class SomeStore{ @observable sum = 100; @observable sumPrev = 100; @computed get diff() { return this.sum - this.sumPrev; } constructor(){ observe(this, "sum", (newValue, oldValue) => { this.sumPrev = oldValue; }); } }
update ↓
constructor(){
observe(this, "sum", ({newValue, oldValue}) => {
this.sumPrev = oldValue;
});
}
the listenner's parameter need to use Destructuring or
constructor(){
observe(this, "sum", (change) => {
this.sumPrev = change.oldValue;
});
}
Please, don't encourage the use of observe, it's not recommended and might be removed over time due to its quirky behavior in some cases. Same for the intercept.
This approach is more resilient and understandable for anyone passing by.
class SomeStore{
@observable sum = 100;
@observable sumPrev = 100;
@computed get diff() {
return this.sum - this.sumPrev;
}
@action updateSum(newSum) {
this.prevSum = this.sum
this.sum = newSum
}
}
Also locking the thread because it's very old and only a few people will see the response. If you have something new to add, feel free to discuss it on Gitter, Spectrum or open a new issue.
Most helpful comment
If I got right the only way is to like this: