1
import { Vue, VueConstructor, Option } from './interfaces'
2 1
import { set, copy } from './objpath'
3 1
import { localStorage } from './drivers'
4 1
import defaultMerge from './merge'
5

6 1
function applyPersistence(vm, option: Option) {
7 1
	const { keys, merge = defaultMerge, namespace: ns, driver = localStorage } = option
8

9 1
	let originaldata = {}
10 1
	for (const k of keys) {
11 1
		copy(originaldata, vm, k)
12
	}
13

14 1
	let data = null
15 1
	if (driver.has(ns)) {
16 1
		data = driver.get(ns)
17
	} else {
18 1
		const tmp = {}
19 1
		for (const k of keys) {
20 1
			copy(tmp, originaldata, k)
21
		}
22 1
		data = tmp
23 1
		driver.set(ns, data)
24
	}
25 1
	data = merge(originaldata, data)
26 1
	for (const k of keys) {
27 1
		copy(vm, data, k)
28 1
		vm.$watch(k, {
29 1
			handler: value => {
30 1
				set(data, k, value)
31 1
				driver.set(ns, data)
32
			},
33
			deep: true
34
		})
35
	}
36
}
37

38 1
export function install(Vue: VueConstructor) {
39 1
	Vue.mixin({
40 1
		created() {
41 1
			if ('storage' in this.$options) {
42 1
				const option: Option | Option[] = this.$options.storage
43 1
				if (Array.isArray(option)) option.forEach(opt => applyPersistence(this, opt))
44 1
				else applyPersistence(this, option)
45
			}
46
		}
47
	})
48
}

Read our documentation on viewing source code .

Loading