Last active
August 29, 2015 14:02
-
-
Save unknownuser88/ca39fc51d20e5690baef to your computer and use it in GitHub Desktop.
variable change event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function watch(target, prop, handler) { | |
if (target.__lookupGetter__(prop) != null) { | |
return true; | |
} | |
var oldval = target[prop], | |
newval = oldval, | |
self = this, | |
getter = function () { | |
return newval; | |
}, | |
setter = function (val) { | |
if (Object.prototype.toString.call(val) === '[object Array]') { | |
val = _extendArray(val, handler, self); | |
} | |
oldval = newval; | |
newval = val; | |
handler.call(target, prop, oldval, val); | |
}; | |
if (delete target[prop]) { // can't watch constants | |
if (Object.defineProperty) { // ECMAScript 5 | |
Object.defineProperty(target, prop, { | |
get: getter, | |
set: setter, | |
enumerable: false, | |
configurable: true | |
}); | |
} else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy | |
Object.prototype.__defineGetter__.call(target, prop, getter); | |
Object.prototype.__defineSetter__.call(target, prop, setter); | |
} | |
} | |
return this; | |
}; | |
function unwatch(target, prop) { | |
var val = target[prop]; | |
delete target[prop]; // remove accessors | |
target[prop] = val; | |
return this; | |
}; | |
// Allows operations performed on an array instance to trigger bindings | |
function _extendArray(arr, callback, framework) { | |
if (arr.__wasExtended === true) return; | |
function generateOverloadedFunction(target, methodName, self) { | |
return function () { | |
var oldValue = Array.prototype.concat.apply(target); | |
var newValue = Array.prototype[methodName].apply(target, arguments); | |
target.updated(oldValue, motive); | |
return newValue; | |
}; | |
} | |
arr.updated = function (oldValue, self) { | |
callback.call(this, 'items', oldValue, this, motive); | |
}; | |
arr.concat = generateOverloadedFunction(arr, 'concat', motive); | |
arr.join = generateOverloadedFunction(arr, 'join', motive); | |
arr.pop = generateOverloadedFunction(arr, 'pop', motive); | |
arr.push = generateOverloadedFunction(arr, 'push', motive); | |
arr.reverse = generateOverloadedFunction(arr, 'reverse', motive); | |
arr.shift = generateOverloadedFunction(arr, 'shift', motive); | |
arr.slice = generateOverloadedFunction(arr, 'slice', motive); | |
arr.sort = generateOverloadedFunction(arr, 'sort', motive); | |
arr.splice = generateOverloadedFunction(arr, 'splice', motive); | |
arr.unshift = generateOverloadedFunction(arr, 'unshift', motive); | |
arr.__wasExtended = true; | |
return arr; | |
} | |
/* | |
var data = { | |
quantity: 0 | |
, products: [] | |
} | |
, watcher = function(propertyName, oldValue, newValue){ … update some other pieces of the application … }; | |
watch(data, 'quantity', watcher); | |
watch(data, 'products', watcher); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment