# Code coverage report for lib/template/vnode/block/binding.js

## Statements: 96% (24 / 25) Branches: 90% (9 / 10) Functions: 100% (6 / 6) Lines: 95.65% (22 / 23) Ignored: none

All files » lib/template/vnode/block/ » binding.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
    661

    1
    452
    452
    452
    452
    452

    1

    468

    468
    11994
    13
    13

    468
    468

    13

    476
    476
    476

    19
    18
    18

     var protoclass = require("protoclass"),
    utils          = require("../../../utils"),
    _bind          = require("../../../utils/bind");

    /**
     */

    function BlockBinding(node, script, view) {
      this.view   = view;
      this.nodeFactory = view.template.nodeFactory;
      this.script = script;
      this.node   = node;
      this.didChange = _bind(this.didChange, this);
    }

    /**
     */

    module.exports = protoclass(BlockBinding, {

      /**
       */

      bind: function() {
        var self = this;

        // TODO - needs to update on rAF
        this.binding = this.script.bind(this.view, function(value, oldValue) {
          if (value === self.currentValue) return;
          self.currentValue = value;
          self.didChange();
        });

        this.currentValue = this.script.evaluate(this.view);
        if (this.currentValue != null) this.update();
      },

      /**
       */

      didChange: function() {
        this.view.runloop.deferOnce(this);
      },

      /**
       */

      update: function() {
        var v = String(this.currentValue == null ? "" : this.currentValue);
        Eif (this.nodeFactory.name !== "dom") {
          this.node.replaceText(v, true);
        } else {
          this.node.nodeValue = String(v);
        }
      },

      /**
       */

      unbind: function() {
        if (this.binding) {
          this.binding.dispose();
          this.binding = void 0;
        }
      }
    });