leaksterrr
leaksterrr

Reputation: 4166

Knockout Computed - write a new value

I'm building a very number/options heavy application using Knockout and on the screen I'm currently working on is pretty complex in terms of what's happening behind the scenes. The whole screen is dynamic and the options displayed are generated from an observableArray.

The way the app works is pretty complex and I've created a slimmed down JSFiddle for you to view here: http://jsfiddle.net/z56zV/

Whenever the button is clicked, I want getValueOne to reset to 0 but no matter what I try, nothing seems to writing the value to getValueOne.

ps. I know you shouldn't really submit a SO post with no code but I feel the best way for you to udnerstand what exactly is going on behind the scenes is to play with the JSFiddle.

Update: I have the code working to an extent, where the write function of getValueOne is reading the value being sent to the computed but I'm having difficulty storing this as the new value? http://jsfiddle.net/z56zV/1/

Upvotes: 0

Views: 92

Answers (1)

Andy Clarke
Andy Clarke

Reputation: 3262

The computed is calculating stuff for you - you can perform some action on the write, but it isn't in itself a variable you can write to.

To clear the computed, you'd have to clear the underlying data ...

self.resetValueToZero = function () {
    for (var i = 0; i < self.computedValues().length; i++) {
        self.computedValues()[0].value(0);
    }
};

Fiddle Demo

Upvotes: 2

Related Questions