Code coverage report for lib/accessors/pojo.js

Statements: 91.3% (63 / 69)      Branches: 72% (18 / 25)      Functions: 94.12% (16 / 17)      Lines: 92.31% (60 / 65)      Ignored: none     

All files » lib/accessors/ » pojo.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 178 179 180 181 182 183 184 185 186 187 1881 1   1 16 16 16 16     1         10             2   2 2 2 2 2     2 2 1 1   1                 26 26   26 10       26 26                     4   4   4   4               9 9 9   9 9 9 7 7 7 7                 10   10       4 4       10   10               2 1     1                   1   1   2   2             2 2 2                               1               6                 11 11          
var BaseAccessor = require("./base");
var _set         = require("../utils/set");
 
function POJOAccessor() {
  BaseAccessor.call(this);
  this._getters  = {};
  this._callers  = {};
  this._watchers = [];
}
 
module.exports = BaseAccessor.extend(POJOAccessor, {
 
  /**
   */
 
  castObject: function(object) { return object; },
 
  /**
   */
 
  call: function(object, path, params) {
 
    var caller;
 
    Eif (!(caller = this._callers[path])) {
      var ctxPath = ["this"].concat(path.split("."));
      ctxPath.pop();
      ctxPath = ctxPath.join(".");
      caller = this._callers[path] = new Function("params", "return this." + path + ".apply(" + ctxPath + ", params);");
    }
 
    try {
      var ret = caller.call(object, params);
      this.applyChanges();
      return ret;
    } catch (e) {
      return void 0;
    }
  },
 
  /**
   */
 
  get: function(object, path) {
 
    var pt = typeof path !== "string" ? path.join(".") : path;
    var getter;
 
    if (!(getter = this._getters[pt])) {
      getter = this._getters[pt] = new Function("return this." + pt);
    }
 
    // is undefined - fugly, but works for this test.
    try {
      return getter.call(object);
    } catch (e) {
      return void 0;
    }
  },
 
  /**
   */
 
  set: function(object, path, value) {
 
    Eif (typeof path === "string") path = path.split(".");
 
    var ret = _set(object, path, value);
 
    this.applyChanges();
 
    return ret;
  },
 
  /**
   */
 
  watchProperty: function(object, path, listener) {
 
    var self = this;
    var currentValue;
    var firstCall = true;
 
    return this._addWatcher(function() {
      var newValue = self.get(object, path);
      if (!firstCall && newValue === currentValue && typeof newValue !== "function") return;
      firstCall = false;
      var oldValue = currentValue;
      currentValue = newValue;
      listener(newValue, currentValue);
    });
  },
 
  /**
   */
 
  _addWatcher: function(applyChanges) {
 
    var self = this;
 
    var watcher = {
      apply: applyChanges,
      trigger: applyChanges,
      dispose: function() {
        var i = self._watchers.indexOf(watcher);
        if (~i) self._watchers.splice(i, 1);
      }
    };
 
    this._watchers.push(watcher);
 
    return watcher;
  },
 
  /**
   */
 
  watchEvent: function(object, event, listener) {
 
    if (Object.prototype.toString.call(object) === "[object Array]" && event === "change") {
      return this._watchArrayChangeEvent(object, listener);
    }
 
    return {
      dispose: function() { }
    };
  },
 
  /**
   */
 
  _watchArrayChangeEvent: function(object, listener) {
 
    var copy = object.concat();
 
    return this._addWatcher(function() {
 
      var hasChanged = object.length !== copy.length;
 
      Iif (!hasChanged) {
        for (var i = 0, n = copy.length; i < n; i++) {
          hasChanged = (copy[i] !== object[i]);
          if (hasChanged) break;
        }
      }
 
      Eif (hasChanged) {
        copy = object.concat();
        listener();
      }
    });
  },
 
  /**
   */
 
  normalizeCollection: function(collection) {
    return collection;
  },
 
  /**
   */
 
  normalizeObject: function(object) {
    return object;
  },
 
  /**
   * DEPRECATED
   */
 
  apply: function() {
    this.applyChanges();
  },
 
  /**
   */
 
  applyChanges: function() {
    // if (this.__applyingChanges) return;
    // this.__applyingChanges = true;
    for (var i = 0, n = this._watchers.length; i < n; i++) {
      this._watchers[i].apply();
    }
    // this.__applyingChanges = false;
  }
});