SteveSanderson/knockout — Issue #180: Add support for formatted observables
Author: rosenfeld
Created: 2011-10-20 18:16:13+00:00
State: open

Suppose a boolean observable:

viewModel.myBoolean = ko.observable(false)

I want myBoolean() to return false, but want data-bind="text: myBoolean" to use "No" instead.

Not sure what would be the best way to design this API, but here are some alternatives:

ko.observable(false).extend(myBooleanFormatter)

myBooleanFormattter could introduce a new "formatted" function to do the job. The convention in the views would be to call any formatted() method on the observable if the method exists, instead of calling toString() in the value of the observable.

For simplifying this, an option could be:

ko.observable(false).formatter(myBooleanFormatter)

where myBooleanFormatter would get the raw value and return a string representation.

This would allow me to have clean views using data-bind="text: myBoolean" instead of data-bind="text: myBoolean().formatted()".

Does it make sense?