赞
踩
新建项目 -> 导入src文件 -> 预览效果
proxy.js
const nativePage = Page
const nativeComponent = Component
Page = (options, key = 'onLoad') => {
const native = options[key]
options[key] = function () {
this.ctx = proxy.call(this)
return native && native.call(this)
}
key === 'onLoad' ? nativePage(options) : nativeComponent(options)
}
Component = options => Page(options, 'created')
function proxy() {
let pending = false
const setData = () => {
pending = true
setTimeout(() => {
this.setData(this.data, () => pending = false)
})
}
const handler = {
get(target, key, receiver) {
try {
if (typeof target[key] === 'function') return Reflect.get(target, key, receiver)
return new Proxy(target[key], handler)
} catch (err) {
return Reflect.get(target, key, receiver)
}
},
set(target, key, value, receiver) {
if (!(Array.isArray(target) && key !== 'length')) !pending && setData()
return Reflect.set(target, key, value, receiver)
},
deleteProperty(target, key) {
!pending && setData()
return Reflect.deleteProperty(target, key)
}
}
return new Proxy(this.data, handler)
}
require('./vendor/proxy.js')
在 index 页面中的 index.js 中使用
onLoad: function () {
this.ctx.motto = 'Hello Echo'
this.ctx.user.name = 'Echo'
}
在 movable 组件中的 movable.js 中使用
created: function () {
this.ctx.visible = true
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。