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

## Statements: 80.36% (45 / 56) Branches: 52.63% (10 / 19) Functions: 90.91% (10 / 11) Lines: 80.39% (41 / 51) 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
    135
    136
    137
    138
    1391

    1

    255
    255
    255
    255
    255
    255
    255

    255
    227

    255

    1

    293

    293

    293
    293

    293
    314

    72
    58

    314
    314
    314

    12
    12

    29
    28
    28

    29
    29
    29

    178

    178

    178

    147
    147
    42

    147

    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 (context) {

        if (this.context) this.unbind();

        Iif (!context) context = {};

        // must wrap around a bindable object
        if (!context.__isBindable) context = this._bindableObject(context);
        this.context = context;

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

      /**
       */

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

      /**
       */

      render: function () {
        if (!this.context) 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();
      },

      /**
       * creates a new bindable object, but syncs any changes back to the original object.
       */

      _bindableObject: function (context) {
        var b = new BindableObject(context);
        b.on("change", function (k, v) {
          context[k] = v;
        });
        return b;
      }
    });

    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;