# Code coverage report for lib/components/switch.js

## Statements: 100% (35 / 35) Branches: 93.75% (15 / 16) Functions: 100% (5 / 5) Lines: 100% (33 / 33) 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
    87
    88
    891

    1
    9

    9

    9
    19

    1

    12

    12
    12
    12
    26
    26
    19

    4
    6

    61

    61
    94
    94

    94
    58

    61

    44
    44

    44

    17
    6

    17

    14

    14

    14
    14

     var BaseComponent  = require("./base"),
    _bind              = require("../utils/bind");

    /**
     */

    function SwitchComponent(options) {
      BaseComponent.call(this, options);

      var self = this;

      // TODO - this is a bit fugly
      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.bind(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) {

          Eif (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;

        // bypass the show component
        // TODO - not optimial. Do on initialize
        var childChildTemplate = child.child(child.vnode.children, {
          accessor: this.view.accessor
        });

        this._view = childChildTemplate.view(this.view.context);
        this.section.appendChild(this._view.render());
      }
    });