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

## Statements: 96.67% (58 / 60) Branches: 95.83% (23 / 24) Functions: 93.75% (15 / 16) Lines: 98.18% (54 / 55) Ignored: 1 statement, 1 branch

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
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    1841
    1
    1
    1
    1

    1

    325
    325
    325
    325
    325
    325
    325
    325
    325
    325

    325
    296

    325

    1

    339
    339

    434
    434

    115

    32

    40
    40

    10

    287

    355
    30

    355

    355

    355
    386

    77
    66

    388
    388
    388

    14
    14

    37
    35
    35

    37
    37
    37

    219

    219
    1

    219

    1
     var protoclass     = require("protoclass");
    var Transitions    = require("./transitions");
    var _bind          = require("../../utils/bind");
    var _stringifyNode = require("../../utils/stringifyNode");
    var Reference      = require("./reference");

    /**
     * constructor
     * @param template the template which created this view
     * @param pool the pool of views to push back into after
     * this view has been disposed
     * @param section the section (cloned node) to attach to
     * @param hydrators binding hydrators that help tie this view
     * to the section
     */

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

      // 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.parent          = options.parent;
      this.accessor        = this.parent ? this.parent.accessor : template.accessor || new template.accessorClass();
      this.rootNode        = section.rootNode();
      this.transitions     = new Transitions();
      this.runloop         = template.runloop;
      this._watchers       = [];

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

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

    /**
     */

    protoclass(View, {

      /**
       */

      setOptions: function(options) {
        this.parent = options.parent;
        if (options.parent) this.accessor = this.parent.accessor;
      },

      /**
       */

      get: function(path) {
        var v = this.accessor.get(this.context, path);
        return v != null ? v : this.parent ? this.parent.get(path) : void 0;
      },

      /**
       */

      set: function(path, value) {
        return this.accessor.set(this.context, path, value);
      },

      /**
       */

      reference: function(path, settable, gettable) {
        return new Reference(this, path, settable, gettable);
      },

      /**
       */

      call: function(path, params) {
        var has = this.accessor.get(this.context, path);
        return has ? this.accessor.call(this.context, path, params) : this.parent ? this.parent.call(path, params) : void 0;
      },

      /**
       */

      setProperties: function(properties) {
        for (var key in properties) this.set(key, properties[key]);
      },

      /**
       */

      watch: function(keypath, listener) {
        return this.accessor.watchProperty(this.context, keypath, listener);
      },

      /**
       */

      watchEvent: function(object, event, listener) {
        return this.accessor.watchEvent(object, event, listener);
      },

      /**
       */

      bind: function(context) {

        if (this.context) {
          this.unbind();
        }
        Iif (!context) context = {};

        this.context = this.accessor.castObject(context);

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

      /**
       */

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

      },

      /**ch
       */

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

        /* istanbul ignore if */
        Iif (this.template.nodeFactory.name === "dom") {
          return _stringifyNode(node);
        }

        return node.toString();
      }
    });

    module.exports = View;