title: KnockoutJS Currency Binding with Real-time Updating
body: <p>In a recent Knockout interface I've been building, I've attempted to set up auto save. It's designed so that saves occur 1 second after you stop making changes. </p>
  
  <p>Here's the problem. By default, KO relies on the blur event on inputs to update the viewModel. My interface doesn't save the data if you leave the page or refresh your browser before clicking out of the box. </p>
  
  <p>To solve this, I've enabled <code>valueUpdate: 'afterkeydown'</code>, which works for all string inputs. However, I have a numeric PricePerLF field that is extended so that it always contains a numeric value. I used the suggested extender found <a href="http://knockoutjs.com/documentation/extenders.html" rel="nofollow">here</a>.</p>
  
  <pre><code>ko.extenders.numeric = function(target, precision) {
      //create a writeable computed observable to intercept writes to our observable
      var result = ko.computed({
          read: target,  //always return the original observables value
          write: function(newValue) {
              var current = target(),
                  roundingMultiplier = Math.pow(10, precision),
                  newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
                  valueToWrite = Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;
  
              //only write if it changed
              if (valueToWrite !== current) {
                  target(valueToWrite);
              } else {
                  //if the rounded value is the same, but a different value was written, force a notification for the current field
                  if (newValue !== current) {
                      target.notifySubscribers(valueToWrite);
                  }
              }
          }
      });
  
      //initialize with current value to make sure it is rounded appropriately
      result(target());
  
      //return the new computed observable
      return result;
  };
  </code></pre>
  
  <p>How would I make this numeric extender play nice with <code>valueUpdate: 'afterkeydown'</code>? It'd also be nice to force it to display a certain number of decimal points such as <code>2.80</code> instead of <code>2.8</code>.</p>