Knockout writable computed observables

leaksterrr

I'm currently building a very number-heavy application using Knockout and I've ran into a problem with making a computed observable writable.

Currently I have a dropdown option where the user selects an age group and the selected value from this populates the text input next to it to show the population for that age group which is a computed observable. Both the dropdown and the text input values are populated from an array.

I have set up a codepen below but basically you'll see that the text input's value is generated via a computed observable and the correct formatting (via a custom binding handler!) is applied when you select a value from the dropdown.

The problem I'm having is that if you enter a custom value into the text input (inputted straight into the textbox as opposed to selecting a value from the dropdown) then when you blur out of the textbox, the custom formatting isn't applied to the value that you input and I'm pretty unsure how to go about tackling this problem.

self.selectedPopulation = ko.computed({

    read: function () {
      return self.chosenAge().population;
    },

    write: function (value) {
      // write value back here?
    },

  });

Codepen: http://codepen.io/anon/pen/HkguL (write function is at Line 76.)

Paul Manzotti

You've need to make the selectedPopulation a normal observable, and subscribe to the chosenAge observable to be notified when it changed.

You'll need to create chosenAge with a dummy object to stop knockout blowing up on binding:

self.chosenAge = ko.observable({age: '', population: ''});

And then change your computed to an observable:

// Selected population
self.selectedPopulation = ko.observable('');

And subscribe to the chosenAge observable:

self.chosenAge.subscribe(function (newValue) {
    self.selectedPopulation(newValue.population);
});

I've not used CodePen before, but I've updated your code here in case I've missed anything in my working example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Knockout writable computed observables

From Dev

Computed Observables with Revealing Prototype Pattern in Knockout JS

From Dev

What is the analog for Knockout's writable computed observable in AngularJS?

From Dev

Knockout: Cannot map computed observables after an Ajax call

From Dev

Knockout Computed with Parameter not updated

From Dev

Knockout Computed Observable with parameters

From Dev

Knockout computed property not binding

From Dev

knockout foreach computed value

From Dev

knockout observable array computed

From Dev

knockout foreach computed value

From Dev

Knockout computed property not binding

From Dev

Knockout observables and performances

From Dev

Sorting a list of observables in knockout

From Dev

Object scoping using computed observables

From Dev

When to use computed/observables in mobx

From Dev

Object scoping using computed observables

From Dev

observable and computed are not working properly Knockout

From Dev

Knockout custom binding to a computed column

From Dev

Knockout Js computed not working in a model

From Dev

Knockout js Computed not being fired

From Dev

Knockout computed observable with access to observableArray

From Dev

Knockout Modifying ObservableArray inside Computed

From Dev

Inheritance and overriding Knockout computed observable

From Dev

Knockout ViewModel computed garbage collection

From Dev

Subscribe arrayChange on knockout computed accessor

From Dev

Knockout ViewModel computed garbage collection

From Dev

Knockout js Computed not being fired

From Dev

Knockout Modifying ObservableArray inside Computed

From Dev

Knockout Computed - write a new value

Related Related

HotTag

Archive