superdata
Version:
A lightweight data layer module motivated by extjs' data layer. It can be used with any client-side framework.
43 lines (32 loc) • 831 B
JavaScript
/*jslint node: true */
;
module.exports = function createProp(obj, name, config) {
//should be called field
config = config || {};
var initialValue = config.value;
var value = initialValue;
var lastValue = value;
Object.defineProperty(obj, name, {
enumerable: true,
configurable: false,
set: set,
get: get
});
function set(newVal) {
if (newVal === value) {
return;
}
if (typeof config.beforeChange === "function") {
config.beforeChange({lastValue: lastValue, value: value, newValue: newVal, initialValue: initialValue});
}
lastValue = value;
value = newVal;
if (typeof config.afterChange === "function") {
config.afterChange({lastValue: lastValue, value: value, newValue: newVal, initialValue: initialValue});
}
}
function get() {
return value;
}
return obj;
};