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

Statements: 81.48% (22 / 27)      Branches: 40% (4 / 10)      Functions: 83.33% (5 / 6)      Lines: 84% (21 / 25)      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 1         1 3 3 3 3 3           1           3   3 3 3 3     3 3             3             6 6 6                                
var protoclass = require("protoclass");
var utils      = require("../../../utils");
var _bind      = require("../../../utils/bind");
 
/**
 */
 
function BlockBinding(node, script, view) {
  this.view   = view;
  this.document = view.template.document;
  this.script = script;
  this.node   = node;
  this.didChange = _bind(this.didChange, this);
}
 
/**
 */
 
module.exports = protoclass(BlockBinding, {
 
  /**
   */
 
  bind: function() {
    var self = this;
 
    this.binding = this.script.watch(this.view, function(value, oldValue) {
      Iif (value === self.currentValue) return;
      self.currentValue = value;
      self.didChange();
    });
 
    this.currentValue = this.script.evaluate(this.view);
    Eif (this.currentValue != null) this.update();
  },
 
  /**
   */
 
  didChange: function() {
    this.view.runloop.deferOnce(this);
  },
 
  /**
   */
 
  update: function() {
    var v = String(this.currentValue == null ? "" : this.currentValue);
    Eif (this.document !== global.document) {
      this.node.replaceText(v, true);
    } else {
      this.node.nodeValue = String(v);
    }
  },
 
  /**
   */
 
  unbind: function() {
    if (this.binding) {
      this.binding.dispose();
      this.binding = void 0;
    }
  }
});