# Code coverage report for lib/components/repeat.js

## Statements: 90.48% (38 / 42) Branches: 88.89% (16 / 18) Functions: 66.67% (4 / 6) Lines: 91.89% (34 / 37) Ignored: none

All files » lib/components/ » repeat.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
    941

    1

    1
    18
    62

    4

    1

    18

    18
    18
    18
    18

    18

    18
    2

    18

    18
    18
    18

    18

    18

    66
    66
    66
    66

    66
    20

    20
    16

    46

    46
    46

    66

    18

     var BaseComponent  = require("./base");

    /**
     * TODO: alloc property
     * TODO: chunk property
     */

    function RepeatComponent(options) {
      BaseComponent.call(this, options);
    }

    /**
     */

    function _each(target, iterate) {
      if (Object.prototype.toString.call(target) === "[object Array]") {
        for (var i = 0, n = target.length; i < n; i++) iterate(target[i], i);
      } else {
        for (var key in target) iterate(target[key], key);
      }
    }

    /**
     */

    module.exports = BaseComponent.extend({

      /**
       */

      // TODO - unbind here

      update: function() {

        if (this._updateListener) this._updateListener.dispose();

        var name     = this.attributes.as;
        var key      = this.attributes.key || "index";
        var source   = this.attributes.each;
        var accessor = this.view.accessor;

        Iif (!source) source = [];

        // note - this should get triggered on rAF
        this._updateListener = accessor.watchEvent(source, "change", function() {
          self.view.runloop.deferOnce(self);
        });

        source = accessor.normalizeCollection(source);

        if (!this._children) this._children = [];
        var self = this;
        var properties;

        var n = 0;

        _each(source, function(model, i) {

          Eif (name) {
            properties = {};
            properties[key]  = i;
            properties[name] = model;
          } else {
            properties = model;
          }

          if (i < self._children.length) {
            var c = self._children[i];

            // model is different? rebind. Otherwise ignore
            if (c.context === model || c.context[name] !== model) {
              c.bind(properties);
            }
          } else {

            // cannot be this - must be default scope
            var child = self.childTemplate.view(properties, {
              parent: self.view
            });

            self._children.push(child);
            self.section.appendChild(child.render());
          }

          n++;
        });

        // TODO - easeOutSync?
        this._children.splice(n).forEach(function(child) {
          child.dispose();
        });
      }
    });