write to computed observable

42 views
Skip to first unread message

Arthur Enokyan

unread,
Oct 14, 2016, 11:55:45 AM10/14/16
to KnockoutJS
i have a computed observable like this

    this.a = ko.observable(1);

    this.b = ko.observable(2);

    this.cc = ko.pureComputed({
            read: function () {
                return parseFloat(self.a() * self.b()).toFixed10(2);
            },
            write: function (value) {
                //?????????
            }
        });


how to write ? help please...

Ian Yates

unread,
Oct 17, 2016, 8:41:57 AM10/17/16
to KnockoutJS
Can you explain what you want to do?  
Pretend Knockout isn't involved...  If I set cc to 6 (for example), what should that do to the values of a & b?

Once you know that, it's a matter of just storing the values in a & b.  That's done by using  this.a(2)  and this.b(3)    to store 2 & 3 in observables a & b respectively.

Arthur Enokyan

unread,
Oct 17, 2016, 12:17:10 PM10/17/16
to KnockoutJS
i wonna do that

cc(6) => c() === 6, and do nothing with a and b 

and 
a(3), b(3) = > c() === 9

понедельник, 17 октября 2016 г., 15:41:57 UTC+3 пользователь Ian Yates написал:

Michael Best

unread,
Oct 17, 2016, 6:03:04 PM10/17/16
to KnockoutJS
You wouldn't then use a computed observable. I'd use three plain observables and subscribe to `a` and `b` to update `c`:

function updateCC() {
    cc
(
parseFloat(self.a() * self.b()).toFixed10(2));
}
this.a.subscribe(updateCC);
this.b.subscribe(updateCC);

Reply all
Reply to author
Forward
0 new messages