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

## Statements: 100% (34 / 34) Branches: 100% (16 / 16) Functions: 100% (5 / 5) Lines: 100% (32 / 32) 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
    861

    1
    9

    9

    9
    19

    1

    10

    10
    10
    10
    20
    20
    15

    2
    2

    22
    41
    41

    41
    19

    22

    5
    1

    5

    17
    6

    17

    14

    14

    14
    14

     var BaseComponent  = require("./base"),
    BindableCollection = require("bindable-collection"),
    BindableObject     = require("bindable-object"),
    _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 (context) {
        BaseComponent.prototype.bind.call(this, context);

        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.runner, context, update));
        }
      },

      /**
       */

      unbind: function () {
        for (var i = this.bindings.length; i--;) {
          this.bindings[i].dispose();
        }
      },

      /**
       */

      update: function () {

        for (var i = 0, n = this.childTemplates.length; i < n; i++) {
          var child = this.childTemplates[i];
          var when = child.vnode.attributes.when;

          if (!when || when.evaluate(this.context)) {
            break;
          }
        }

        if (this.currentChild == child) {

          if (this._view && this._view.context !== this.context) {
            this._view.bind(this.context);
          }

          return;
        }

        if (this._view) {
          this._view.dispose();
        }

        if (i == n) return;

        this.currentChild = child;

        // bypass the show component
        var childChildTemplate = child.child(child.vnode.children);

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