Skip to content

Instantly share code, notes, and snippets.

@superMDguy
Last active September 25, 2017 20:36
Show Gist options
  • Save superMDguy/08c5ab5bac949e4e39776b98f43a5534 to your computer and use it in GitHub Desktop.
Save superMDguy/08c5ab5bac949e4e39776b98f43a5534 to your computer and use it in GitHub Desktop.
// See https://github.com/vuejs/vue/blob/61187596b9af48f1cb7b1848ad3eccc02ac2509d/src/core/observer/index.js
// for full implementation
/* Walk through each property and convert them into
* getter/setters. This method should only be called when
* value type is Object.
*/
function walk(obj) {
const keys = Object.keys(obj)
for (let i = 0; i < keys.length; i++) {
defineReactive(obj, keys[i], obj[keys[i]])
}
}
function defineReactive(obj, key, val) {
if (val !== null && typeof val === 'object') {
walk(val) // Add reactivity to all children of val
}
const dep = new Dep()
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter() {
dep.depend()
return val
},
set: function reactiveSetter(newVal) {
val = newVal
dep.notify()
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment