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
| export function setWatcher(page) { let data = page.data; let watch = page.watch; Object.keys(watch).forEach(v => { let key = v.split('.'); let nowData = data; for (let i = 0; i < key.length - 1; i++) { nowData = nowData[key[i]]; } let lastKey = key[key.length - 1]; let watchFun = watch[v].handler || watch[v]; let deep = watch[v].deep; observe(nowData, lastKey, watchFun, deep, page); }) } function observe(obj, key, watchFun, deep, page) { var val = obj[key]; if (deep && val != null && typeof val === 'object') { Object.keys(val).forEach(childKey => { observe(val, childKey, watchFun, deep, page); }) } let that = this; Object.defineProperty(obj, key, { configurable: true, enumerable: true, set: function (value) { watchFun.call(page, value, val); val = value; if (deep) { observe(obj, key, watchFun, deep, page); } }, get: function () { return val; } }) } module.exports = { setWatcher: setWatcher }
|