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

## Statements: 94.23% (49 / 52) Branches: 90% (18 / 20) Functions: 93.75% (15 / 16) Lines: 95.74% (45 / 47) 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
    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
    1781

    1

    599
    599
    599
    599
    599
    599
    599
    599

    599
    572

    599

    1

    604

    15736
    15736

    71

    124

    68
    68

    10

    638

    668
    668

    668

    668
    651

    102
    50

    675
    675
    675

    54
    54

    28
    27
    27

    28
    28
    28

    184

    184

    184

    1var protoclass = require("protoclass"),
    Transitions    = require("./transitions"),
    _bind          = require("../../utils/bind"),
    _stringifyNode = require("../../utils/stringifyNode"),
    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) {

      // 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.accessor        = template.accessor || new template.accessorClass();
      this.rootNode        = section.rootNode();
      this.transitions     = new Transitions();
      this.runloop         = template.runloop;

      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;
      },

      /**
       */

      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) {
        return new Reference(this, path, settable);
      },

      /**
       */

      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(path, listener) {
        return this.accessor.watchProperty(this.context, path, 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();

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

        return node.toString();
      }
    });

    module.exports = View;