I suggest you ...

MVVM: Writeable calculated fields, and/or set events for observable properties

As seen in KOjs: Writeable computed observables
http://knockoutjs.com/documentation/computedObservables.html

Or, subscribing to a set event on an observable:
http://knockoutjs.com/documentation/observables.html (bottom section: Explicitly subscribing to observables)

74 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Andrew CAndrew C shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    3 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Simon GreenSimon Green commented  ·   ·  Flag as inappropriate

        One place we used writable computeds with Knockout was in input fields used to capture large numerical monetary values. Users wanted to be able to input '10m' to represent 10 million. So, we bound the input to a computed. When the user entered 10m, it parsed the string into 10000000 and set that on the underlying observable (as a number).

        However, was also wanted the computed to work in the other direction, so that when the page was loaded, we set the value on the observable (as 10000000), which was then formatted to 10m by the computed.

        We could have done the parse and format server-side, but was also needed the numerical value client side, for validation (e.g make sure number wasn't bigger than another number that had been entered elsewhere)

        Finally, the bidrectional computed helped in this situation: User types in 10000000, the computed parses this (as 10000000) and sets it on the observable, which triggers a change in the observable, which causes the computed to re-compute - as '10M' - which updated the text box after the uses moved out of it. So it implemented an auto-formatting of numbers that users typed in

      • Anonymous commented  ·   ·  Flag as inappropriate

        Following scenario: In my viewModel i have a field with type Date. How do I parse input from user back? with writable dependent it would be possible:
        var viewModel = {
        value : new Date();
        displayValue: { getFunction(){
        return //format value to string;
        },
        setFunction(){
        value = //parse entered string;
        }

        }

      • Brandon SatromAdminBrandon Satrom (Program Manager, Kendo UI, Telerik Kendo UI) commented  ·   ·  Flag as inappropriate

        Hi Andrew,

        Can you give me a scenario where a writable computed observable would be required? As the KO site states, these are very uncommon, mainly because the computed nature of the observable makes them unnecessary. If you need an update, set one or more of the dependent observables, and the computer observable also reflects the change.

      Feedback and Knowledge Base