# .onvaluechanged use

**URL:** <https://forums.fusetools.com/t/onvaluechanged-use/6840>\
**Category:** How-to Discussions\
**Created:** [May 5, 2017, 11:44am UTC](https://forums.fusetools.com/t/onvaluechanged-use/6840 "2017-05-05T11:44:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@paul](https://forums.fusetools.com/u/paul)\
**Post date:** [May 5, 2017, 11:44am UTC](https://forums.fusetools.com/t/onvaluechanged-use/6840/1 "2017-05-05T11:44:45Z")

</div>

Hi Im wondering if this use of an .onvaluechanged is legitimate and works as intended

```auto
var amount = Observable();
var buttonEnabled = Observable(false);

amount.onValueChanged(null, function() {
    if(parseInt(amount.value) >= 1000){
        buttonEnabled.value = true;
    }else{
        buttonEnabled.value = false;
    }
});

```

where amount is bound to an input from ux, and buttonEnabled is a button only enabled when the value is over a certain value

```auto
    <TextInput Value="{amount}" />

```

It works, I’m just wondering if youre suppost to use the listner like this, since the onvaluechanged is suppost to listen for changes in a module. Here I am trying to listen for changes in the observable.

Maybe there is some other logic that makes more sense?

---

<div class="post-metadata">

**Author:** ![Uldis](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/uldis/32/657_2.png) [@Uldis](https://forums.fusetools.com/u/Uldis)\
**Post date:** [May 5, 2017, 11:56am UTC](https://forums.fusetools.com/t/onvaluechanged-use/6840/2 "2017-05-05T11:56:51Z")

</div>

Hi Paul,

the approach in general looks legit, although there are some minor things that should be done differently:

```auto
var amount = Observable(0);
var buttonEnabled = Observable(false);

amount.onValueChanged(module, function(x) {
    if (parseInt(x) >= 1000) {
        buttonEnabled.value = true;
    } else {
        buttonEnabled.value = false;
    }
});

```

Note that you get the new value of `amount` observable in the `x` parameter every time there is a change to it.

Also, make sure you always put the first parameter exactly as `module` - it ties the subscription to the lifecycle of the JS file you’re currently in (it is the same `module` as in `module.exports`). Otherwise you risk leaking memory.

---

<div class="post-metadata">

**Author:** ![paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@paul](https://forums.fusetools.com/u/paul)\
**Post date:** [May 5, 2017, 12:09pm UTC](https://forums.fusetools.com/t/onvaluechanged-use/6840/3 "2017-05-05T12:09:43Z")

</div>

but couldnt I listen for changes in a different module?

---

<div class="post-metadata">

**Author:** ![Uldis](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/uldis/32/657_2.png) [@Uldis](https://forums.fusetools.com/u/Uldis)\
**Post date:** [May 5, 2017, 12:18pm UTC](https://forums.fusetools.com/t/onvaluechanged-use/6840/4 "2017-05-05T12:18:11Z")

</div>

Paul, I think you have misunderstood how `.onValueChanged` works. Or the `module` bit.

The `module` there is only to bind the lifecycle of that subscription (`.onValueChanged` adds a subscriber to the Observable) to that particular JS file. So when you, say, navigate away from the page where you use it, the subscriber would also be killed, so that it doesn’t stay lingering around and eating memory in background.

`obs.onValueChanged` does listen to the changes of that Observable. And that’s all it does.

---

<div class="post-metadata">

**Author:** ![paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@paul](https://forums.fusetools.com/u/paul)\
**Post date:** [May 5, 2017, 12:30pm UTC](https://forums.fusetools.com/t/onvaluechanged-use/6840/5 "2017-05-05T12:30:25Z")

</div>

Oh! ok thank you for the thorough explaination
