Code coverage report for lib/components/switch.js

Statements: 11.11% (4 / 36)      Branches: 0% (0 / 16)      Functions: 0% (0 / 5)      Lines: 11.76% (4 / 34)      Ignored: none     

All files » lib/components/ » switch.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 871 1         1                         1                                                                                                                                      
var BaseComponent = require("./base");
var _bind         = require("../utils/bind");
 
/**
 */
 
function SwitchComponent(options) {
  BaseComponent.call(this, options);
 
  var self = this;
 
  this.childTemplates = this.childTemplate.vnode.children.map(function(vnode) {
    return self.childTemplate.child(vnode);
  });
}
 
/**
 */
 
module.exports = BaseComponent.extend(SwitchComponent, {
 
  /**
   */
 
  bind: function() {
    BaseComponent.prototype.bind.call(this);
 
    this.bindings = [];
    var update = _bind(this.update, this);
    for (var i = 0, n = this.childTemplates.length; i < n; i++) {
      var when = this.childTemplates[i].vnode.attributes.when;
      if (!when) continue;
      this.bindings.push(when.watch(this.view, this.didChange));
    }
  },
 
  /**
   */
 
  unbind: function() {
    for (var i = this.bindings.length; i--;) {
      this.bindings[i].dispose();
    }
  },
 
  /**
   */
 
  update: function() {
 
    var child;
 
    for (var i = 0, n = this.childTemplates.length; i < n; i++) {
      child = this.childTemplates[i];
      var when = child.vnode.attributes.when;
 
      if (!when || when.evaluate(this.view)) {
        break;
      }
    }
 
    if (this.currentChild == child) {
 
      if (this._view && this._view.context !== this.context) {
        this._view.bind(this.view.context);
      }
 
      return;
    }
 
    if (this._view) {
      this._view.dispose();
    }
 
    if (i == n) return;
 
    this.currentChild = child;
 
    var childChildTemplate = child.child(child.vnode.children, {
      accessor: this.view.accessor
    });
 
    this._view = childChildTemplate.view(this.view.context);
    this.section.appendChild(this._view.render());
  }
});