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

## Statements: 86.67% (26 / 30) Branches: 83.33% (10 / 12) Functions: 50% (2 / 4) Lines: 92.31% (24 / 26) 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
    781

    1

    1

    18

    18

    18

    18

    18
    2

    18

    18
    18

    18

    66

    66
    66

    66
    24

    24
    20

    42
    42
    42

    18

     var BaseComponent  = require("./base"),
    BindableCollection = require("bindable-collection"),
    BindableObject     = require("bindable-object"),
    ScopedBindableObject = require("scoped-bindable-object");

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

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

    /**
     */

    module.exports = BaseComponent.extend({

      /**
       */

      // TODO - unbind here

      update: function () {
        if (this._updateListener) this._updateListener.dispose();

        var name      = this.attributes.as,
        source        = this.attributes.each,
        watcher       = this.view.scope.watcher,
        deserializer  = this.view.scope.deserializer;

        Iif (!name) throw new Error("'as' must exist for repeat block");

        Iif (!source) source = [];

        this._updateListener = watcher.watchCollection(source, "change", function () {
          self.view.runner.run(self);
        });

        source = deserializer.deserializeCollection(source);

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

        for (var i = 0, n = source.length; i < n; i++) {

          var properties, model = source[i];

            var properties = { index: i };
            properties[name] = model;

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

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

            // cannot be this - must be default scope
            // new ScopedBindableObject(properties, this.scope)
            var child = this.childTemplate.view(this.scope.child(properties));
            this._children.push(child);
            this.section.appendChild(child.render());
          }
        }

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