# Computed property is not getting updated

**URL:** <https://discuss.emberjs.com/t/computed-property-is-not-getting-updated/5150>\
**Category:** Uncategorized\
**Created:** [May 6, 2014, 7:57am UTC](https://discuss.emberjs.com/t/computed-property-is-not-getting-updated/5150 "2014-05-06T07:57:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nmk/32/15747_2.png) [@nmk](https://discuss.emberjs.com/u/nmk)\
**Post date:** [May 6, 2014, 7:57am UTC](https://discuss.emberjs.com/t/computed-property-is-not-getting-updated/5150/1 "2014-05-06T07:57:45Z")

</div>

I have a test case in which a computed property is not getting updated when I would expect it to do so.

Here is a JSBin: [http://emberjs.jsbin.com/hawa/30/edit](http://emberjs.jsbin.com/hawa/30/edit)

To reproduce the effect I am seeing change the weight of one of the models by entering a different number, say “30” in the input field and pressing enter. The weight and formattedWeight change as expected.

Now press the “Reset weights” button, which sets the weight of each item in the controller to 50.

The weights in the output are updated correctly, the formattedWeight of the item which was previously set manually is **not** updated.

I can work around this using an observer and manually setting the formattedWeight property when the weight property changes. I would however expect that the property is updated automatically, being dependent on the weight property.

Is this by design, or a bug?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![YIk](https://avatars.discourse-cdn.com/v4/letter/y/a5b964/32.png) [@YIk](https://discuss.emberjs.com/u/YIk)\
**Post date:** [May 6, 2014, 12:10pm UTC](https://discuss.emberjs.com/t/computed-property-is-not-getting-updated/5150/2 "2014-05-06T12:10:54Z")

</div>

You try to use computed property setter as well as getter, here working JSBin: [JS Bin - Collaborative JavaScript Debugging](http://emberjs.jsbin.com/hawa/41/edit)

For using computed property setter as well as getter you can use like

> formattedWeight: function(key, value) {  
> if(arguments.length\>1){ &nbsp;&nbsp;&nbsp;return value; &nbsp;&nbsp; }&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; return this.get(‘weight’).toFixed(2); &nbsp; }.property(‘weight’)

---

<div class="post-metadata">

**Author:** ![zackangelo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/zackangelo/32/14491_2.png) [@zackangelo](https://discuss.emberjs.com/u/zackangelo)\
**Post date:** [May 6, 2014, 4:20pm UTC](https://discuss.emberjs.com/t/computed-property-is-not-getting-updated/5150/3 "2014-05-06T16:20:46Z")

</div>

@YIk is right, you need to need to make the `formattedWeight` property writable because you have it bound to your text field (the `Ember.TextField` view will call `set` on that property as the text in the field changes).

```auto
formattedWeight: function(key,value) {
    if(arguments.length > 1) { 
      return value;
    }
    
    return this.get('weight').toFixed(2);
 }.property('weight')

```

If `formattedWeight` is just going to be a nicely printed version of `weight`, maybe you should consider adding `.readOnly()` to the end of the computed property definition.

```auto
formattedWeight: function(key,value) {
    return this.get('weight').toFixed(2);
 }.property('weight').readOnly()

```

---

<div class="post-metadata">

**Author:** ![nmk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nmk/32/15747_2.png) [@nmk](https://discuss.emberjs.com/u/nmk)\
**Post date:** [May 7, 2014, 9:55am UTC](https://discuss.emberjs.com/t/computed-property-is-not-getting-updated/5150/4 "2014-05-07T09:55:25Z")

</div>

Thanks guys, that was it.

The property cannot be read-only, as the input field is used for setting the weight property.
