wux-weapp
Version:
一套组件化、可复用、易扩展的微信小程序 UI 组件库
49 lines (44 loc) • 1.85 kB
JavaScript
import shallowEqual from '../libs/shallowEqual'
import { isEmpty } from '../shared/isEmpty'
const ALL_DATA_KEY = '**'
const trim = (str = '') => str.replace(/\s/g, '')
export default Behavior({
lifetimes: {
attached() {
this.initComputed()
},
},
definitionFilter(defFields) {
const { computed = {} } = defFields
const observers = Object.keys(computed).reduce((acc, name) => {
const [field, getter] = Array.isArray(computed[name]) ? computed[name] : [ALL_DATA_KEY, computed[name]]
return {
...acc,
[field]: function(...args) {
if (typeof getter === 'function') {
const newValue = getter.apply(this, args)
const oldValue = this.data[name]
if (!isEmpty(newValue) && !shallowEqual(newValue, oldValue)) {
this.setData({ [name]: newValue })
}
}
},
}
}, {})
Object.assign(defFields.observers = (defFields.observers || {}), observers)
Object.assign(defFields.methods = (defFields.methods || {}), {
initComputed: function(data = {}, isForce = false) {
if (!this.runInitComputed || isForce) {
this.runInitComputed = false
const context = this
const result = { ...this.data, ...data }
Object.keys(observers).forEach((key) => {
const values = trim(key).split(',').reduce((acc, name) => ([...acc, result[name]]), [])
observers[key].apply(context, values)
})
this.runInitComputed = true
}
},
})
},
})