Triggering function on observable change?

77 views
Skip to first unread message

Kuroonehalf

unread,
Apr 18, 2018, 8:34:38 PM4/18/18
to KnockoutJS
I feel like this should be simple enough that I should've figured it out by now, but I still haven't had success in getting it to work, so I figure it's better to ask. >.<


I have this data-bound variable called pomolength, and whenever it changes I want it to call a store.set() command so I can store the value and restore it next time the program is opened, effectively making it persistent. What would I have to adapt here to do this?

var vm = {
  pomolength : ko.observable(default_pomo_length),
...
}

Kuroonehalf

unread,
Apr 19, 2018, 4:34:04 PM4/19/18
to KnockoutJS
Got it to work, I think! Instead of binding my input control to an obsersable variable directly, I bound it to a writable computed observable that on write doesn't just edit the variable, but does a new store.set.

Andrew Vickers

unread,
Apr 20, 2018, 6:07:25 PM4/20/18
to KnockoutJS
That is one way to do it.  The other two ways are to create a custom binding that incorporates the persistence logic or to manually subscribe to the pomolength observable.

vm.pomolength.subscribe((newValue) => store.set(newValue))

Kuroonehalf

unread,
Apr 21, 2018, 8:48:22 AM4/21/18
to KnockoutJS
Oh wow, that's so much easier and less verbose. Gonna adapt my code. Thanks!
Reply all
Reply to author
Forward
0 new messages