ko.computed() is not triggered when observable value changes (new to KnockoutJS)

3,486 views
Skip to first unread message

arnaslu

unread,
May 30, 2012, 11:16:13 AM5/30/12
to knock...@googlegroups.com
I'm new to KnockoutJS and JavaScript MVC in general.

I followed some examples from knockoutjs.com and tried to troubleshoot using this forum where people seem to have similar issues but I can't quite nail it.

Basically I have a list (array) or days and values and I want to display total for those values. It works just fine when loading initial data, but if I change some values - data is not updated automatically. I can see that object data is update by exporting it to JSON, but total field is not updated unless I manually refresh it. Here's what I'm working with - http://jsfiddle.net/XpETm/1/

nitinbhide

unread,
May 30, 2012, 12:11:58 PM5/30/12
to knock...@googlegroups.com
In documentation of Observable Array (http://knockoutjs.com/documentation/observableArrays.html), it is mentioned that ' An observableArray tracks which objects are in the array, not the state of those objects'. In your case, you are changing the state of object inside the array (e.g the amount variable of object inside the array). Since there is no changes the size of Observable Array, re-computation of total is not triggered.

I have modified the code a little bit. I have changed 'amount' variable inside observableArray into an observable it self. This triggers the computation of 'weekTotal' any time you edit the amount.  Check http://jsfiddle.net/AjAHx/.

regards,
Nitin

BootStrapToday : The  Sensible Application Lifecycle Management
http://www.bootstraptoday.com (Built with Knockoutjs)

arnaslu

unread,
May 30, 2012, 1:45:51 PM5/30/12
to knock...@googlegroups.com
Ah, I though that might be the problem and I actually tried using ko.observable() on amount field, but then it would print a bunch of garbage, so I decided I was digging in the wrong place. 

Thanks for your help!!
Reply all
Reply to author
Forward
0 new messages