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

## Statements: 97.67% (42 / 43) Branches: 90% (9 / 10) Functions: 100% (10 / 10) Lines: 100% (38 / 38) 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
    1121

    1

    259
    259
    259
    259
    259
    259
    259

    259
    230

    259

    1

    297

    297

    297
    297

    297
    317

    79
    63

    313
    313
    313

    12
    12

    36
    35
    35

    36
    36
    36

    66

    147
    147
    42

    147

    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 () {
        return this.render().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;
      }
    });

    module.exports = View;