title: how to implement checkbox dependencies in knockoutjs
body: <p>I have a group of checkboxes</p>
  
  <ul>
  <li>checkbox A</li>
  <li>checkbox B</li>
  <li>checkbox C</li>
  </ul>
  
  <p>Generated with the foreach data binding:</p>
  
  <pre><code>&lt;input type="checkbox" data-bind="value: id, checked: $root.chkboxSelected" /&gt;
  </code></pre>
  
  <p>Which take their checked state from an observableArray. So checking a box will add the corresponding value to the array, standard knockoutjs that works fine.  I then wanted to add a simple rule:</p>
  
  <p>if C is checked, then A and B must be checked as well.</p>
  
  <p>What is the cleanest way to add this kind of logic in knockoutjs?  I tried with a writable computable observable:</p>
  
  <pre><code>var viewModel = {
        foo: observableArray(),
        ..
      };
  
  viewModel.chkboxSelected = ko.computed({
        read: function() {
          return this.foo();
        },
        write: function(value){
              //add it if not added already
          if($.inArray(value, this.foo()) &lt; 0) {
              this.foo.push(value);
          }
  
              // if C is present then A,B must be as well         
          if($.inArray("C", this.foo()) &gt;= 0) {
            if($.inArray("B", this.foo()) &lt; 0) {
              this.foo().push("B");
            }
  
            if($.inArray("A", this.foo()) &lt; 0) {
                  this.foo().push("A");
            }
  
          }
        },
        owner: viewModel
      });
  </code></pre>
  
  <p>Putting a breakpoint on the read and write functions: read gets called and the page loads fine.  However, when I then click any checkbox I get the following error (the write breakpoint never gets hit):</p>
  
  <pre><code>knockout-2.0.0.debug.js:2297
  
  Uncaught TypeError: Object function dependentObservable() {
          if (arguments.length &gt; 0) {
              if (typeof options["write"] === "function") {
                  // Writing a value
                  var valueForThis = options["owner"] || evaluatorFunctionTarget; // If undefined, it will default to "window" by convention. This might change in the future.
                  options["write"].apply(valueForThis, arguments);
              } else {
                  throw "Cannot write a value to a dependentObservable unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.";
              }
          } else {
              // Reading the value
              if (!_hasBeenEvaluated)
                  evaluateImmediate();
              ko.dependencyDetection.registerDependency(dependentObservable);
              return _latestValue;
          }
      } has no method 'push'
  </code></pre>