|
1 | | -import { hasOwnProperty } from './object' |
| 1 | +import cloneDeep from './clone-deep' |
| 2 | +import looseEqual from './loose-equal' |
| 3 | +import { hasOwnProperty, keys } from './object' |
| 4 | + |
| 5 | +const isEmpty = value => !value || keys(value).length === 0 |
2 | 6 |
|
3 | 7 | export const makePropWatcher = propName => ({ |
4 | | - handler(newVal, oldVal) { |
5 | | - for (const key in oldVal) { |
6 | | - if (!hasOwnProperty(newVal, key)) { |
| 8 | + handler(newValue, oldValue) { |
| 9 | + if (looseEqual(newValue, oldValue)) { |
| 10 | + return |
| 11 | + } |
| 12 | + if (isEmpty(newValue) || isEmpty(oldValue)) { |
| 13 | + this[propName] = cloneDeep(newValue) |
| 14 | + return |
| 15 | + } |
| 16 | + for (const key in oldValue) { |
| 17 | + if (!hasOwnProperty(newValue, key)) { |
7 | 18 | this.$delete(this.$data[propName], key) |
8 | 19 | } |
9 | 20 | } |
10 | | - for (const key in newVal) { |
11 | | - this.$set(this.$data[propName], key, newVal[key]) |
| 21 | + for (const key in newValue) { |
| 22 | + this.$set(this.$data[propName], key, newValue[key]) |
12 | 23 | } |
13 | 24 | } |
14 | 25 | }) |
15 | 26 |
|
16 | 27 | export const makePropCacheMixin = (propName, proxyPropName) => ({ |
17 | 28 | data() { |
18 | | - return { |
19 | | - [proxyPropName]: {} |
20 | | - } |
| 29 | + return { [proxyPropName]: cloneDeep(this[propName]) } |
21 | 30 | }, |
22 | 31 | watch: { |
23 | 32 | // Work around unwanted re-renders: https://github.com/vuejs/vue/issues/10115 |
24 | 33 | [propName]: makePropWatcher(proxyPropName) |
25 | | - }, |
26 | | - created() { |
27 | | - this[proxyPropName] = { ...this[propName] } |
28 | 34 | } |
29 | 35 | }) |
0 commit comments