# Code coverage report for lib/template/view/index.js

## Statements: 79.63% (43 / 54) Branches: 52.63% (10 / 19) Functions: 88.89% (8 / 9) Lines: 80% (40 / 50) Ignored: none

All files » lib/template/view/ » index.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
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    1351

    1

    262
    262
    262
    262
    262
    262
    262

    262
    235

    262

    1

    300

    300

    300

    300
    214

    86

    300

    300
    323

    72
    58

    324
    324
    324

    12
    12

    29
    28
    28

    29
    29
    29

    184

    184

    184

    1

    1var protoclass = require("protoclass"),
    BindableObject = require("bindable-object"),
    Transitions    = require("./transitions"),
    _bind          = require("../../utils/bind");

    function View (template, pool, section, hydrators) {

      // todo - check if node child length is > 1. If so, then
      // create a section, otherwise don't.
      // this.section = node.childNodes.length > 1 ? createSection() : singleSection(this.node);
      this.template        = template;
      this.section         = section;
      this.bindings        = [];
      this._pool           = pool;
      this.runner          = template.runner;
      this.rootNode        = section.rootNode();
      this.transitions     = new Transitions();

      for (var i = 0, n = hydrators.length; i < n; i++) {
        hydrators[i].hydrate(this);
      }

      this._dispose = _bind(this._dispose, this);
    }

    protoclass(View, {

      /**
       */

      bind: function (scopeOrContext) {

        if (this.scope) this.unbind();

        Iif (!scopeOrContext) scopeOrContext = {};

        var scope;

        if (!scopeOrContext.__isScope) {
          scope = new this.template.scopeClass(scopeOrContext);
        } else {
          scope = scopeOrContext;
        }

        // must wrap around a bindable object
        this.scope = scope;

        for (var i = 0, n = this.bindings.length; i < n; i++) {
          this.bindings[i].bind(scope);
        }
      },

      /**
       */

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

      /**
       */

      render: function () {
        if (!this.scope) this.bind({});
        this.transitions.enter();
        return this.section.render();
      },

      /**
       */

      remove: function () {
        this.section.remove();
        return this;
      },

      /**
       * disposes the view, and re-adds it to the template pool. At this point, the
       * view cannot be used anymore.
       */

      dispose: function () {
        if (this.transitions.exit(this._dispose)) return;
        this._dispose();
        return this;
      },

      /**
       */

      _dispose: function () {
        this.unbind();
        this.section.remove();
        this._pool.push(this);
      },

      /**
       */

      toString: function () {
        var node = this.render();

        Iif (this.template.nodeFactory.name === "dom") {
          return _stringifyNode(node);
        }

        return node.toString();
      }
    });

    function _stringifyNode (node) {

      var buffer = "";

      if (node.nodeType === 11) {
        for (var i = 0, n = node.childNodes.length; i < n; i++) {
          buffer += _stringifyNode(node.childNodes[i]);
        }
        return buffer;
      }

      buffer = node.nodeValue || node.outerHTML || "";

      if (node.nodeType === 8) {
        buffer = "<!--" + buffer + "-->";
      };

      return buffer;
    }

    module.exports = View;