title: Use knockout extenders to add warning if value is negative
body: <p>I've gotten an requirement that all numeric columns in our knockout grid should have red color if they are negative to warn the user. </p>
  
  <p>I could do it with standard bindings, but it would add a lot of code to the viewmodel and view.</p>
  
  <p>I use the knockout validation framework and they use extenders to add validation.</p>
  
  <p>I want to do the same, I've looked at the extender examples, but they require you do add elements to the view, Somehow the validation framework works with only extenders, how can I do the same? My goal is to only need something like this to add the warning</p>
  
  <pre><code>this.accountBalance = ko.observable(data.AccountBalance).extend({ warnIfNegative: true });
  </code></pre>
  
  <p><strong>Update:</strong> I did this after looking at the validation framework, what I dont like is that you need to override each binding to get it to work, for example i want this both for text and value binding</p>
  
  <pre><code>(function () {
  
      ko.extenders.negativeValueWarning = function (target, option) {
          target.hasWarning = ko.observable();
  
          function warn(newValue) {
              target.hasWarning(newValue &lt; 0 ? true : false);
          }
  
          warn(target());
  
          target.subscribe(warn);
  
          return target;
      };
  
  
      var init = ko.bindingHandlers.text.init;
      ko.bindingHandlers.text.init = function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  
          if (init)
              init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
  
          var value = valueAccessor();
          if (value.hasWarning) {
              ko.applyBindingsToNode(element, { negativeValueWarning: value.hasWarning });
          }
      };
  
      ko.bindingHandlers.negativeValueWarning = {
          update: function (element, valueAccessor) {
              var warn = ko.utils.unwrapObservable(valueAccessor());
              if (warn)
                  $(element).addClass("negative-warning");
              else
                  $(element).removeClass("negative-warning");
          }
      };
  } ());
  </code></pre>