You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
reagent_manage/vue/full/src/util/observable.js

36 lines
903 B

import {getInitialValue} from "@/util"
//为Vue.observer返回的对象设置getter
export function createGetters(store) {
const getters = Object.create({})
Object.defineProperties(
getters,
Object.keys(store).reduce((obj, key) => {
obj[key] = {
enumerable: true,
get() {
return store[key]
}
}
return obj
}, {})
)
return getters
}
//为Vue.observer返回的对象设置mutation
export function createMutations(store, all = false) {
const keys = Object.keys(store)
const obj = {}
keys.forEach(key => {
obj[key] = v => store[key] = v
})
if (all) {
obj['$all'] = v => {
keys.forEach(key => {
store[key] = v && v[key] || getInitialValue(store[key])
})
}
}
return obj
}