-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpagehelper.js
136 lines (123 loc) · 4.53 KB
/
pagehelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function isObject (key, obj) {
if (typeof obj !== 'object') {
throw new Error(`zachary 抛出:${key} 必须是一个对象`)
}
}
class BasePage {
constructor () {
this._mixins = {}
}
mixinLifeCycle (funcObjc) {
this._mixins = funcObjc
}
mixinAction (funcObjc) {
}
/**
* 生成Page的方法
* @param {object} props 不影响UI对象
* @param {object} data 影响UI对象
* @param {object} lifeCycle 生命周期对象
* @param {object} privateMethod 私有方法对象
* @param {object} viewAction UI点击事件对象
*
*/
register (props = {}, data = {}, lifeCycle = {}, privateMethod = {}, viewAction = {}, computed = {}, watch = {}) {
isObject('props', props) && isObject('data', data) && isObject('lifeCycle', lifeCycle) && isObject('privateMethod', privateMethod) && isObject('viewAction', viewAction) && isObject('computed', computed) && isObject('watch', watch)
let lifeCycleObject = {}
!!lifeCycle && Object.keys(lifeCycle).forEach(key => {
let hasMethod = false
// 如果注册过 mixin 那么先执行 mixin 再执行原始方法
for (const mixKey in this._mixins) {
const element = this._mixins[mixKey]
if (key === mixKey) {
hasMethod = true
lifeCycleObject[key] = function (...param) {
if (key === 'onLoad' && param[0].param) {
param[0] = JSON.parse(decodeURIComponent(param[0].param))
}
element.apply(this, param)
lifeCycle[key].apply(this, param)
}
}
}
// 没有 mixin 的时候,如果有 param 需要先 decode
if (!hasMethod) {
lifeCycleObject[key] = function (...param) {
if (key === 'onLoad' && param[0].param) {
param[0] = JSON.parse(decodeURIComponent(param[0].param))
}
lifeCycle[key].apply(this, param)
}
}
})
const originalOnLoad = lifeCycleObject['onLoad'] || function () {}
const onLoad = function (...onLoadParam) {
// 初始化计算属性
for (const key in computed) {
if (computed.hasOwnProperty(key)) {
const element = computed[key]
const result = element.apply(this, [this.data])
this.setData({ [key]: result })
}
}
// 初始化watch属性
const watchMap = new Map()
for (const key in watch) {
watchMap.set(key, watch[key])
}
// 替换原来的 setData
const originalSetData = this.setData
this.setData = function (...param) {
// 调用原始 setData
originalSetData.apply(this, param)
// setData 的时候设置 watch 属性
for (const key in param[0]) {
const element = param[0][key]
if (watchMap.has(key)) {
watchMap.get(key).apply(this, [element, this.data[key]])
}
}
// setData 的时候设置 computed 属性
for (const key in computed) {
if (computed.hasOwnProperty(key)) {
const element = computed[key]
const result = element.apply(this, [Object.assign({}, this.data, param[0])])
originalSetData.apply(this, [{ [key]: result }])
}
}
}
// 方便使用 setState 调用
this.setState = this.setData
originalOnLoad.apply(this, onLoadParam)
}
lifeCycleObject['onLoad'] = onLoad
let privateMethodObject = {}
!!privateMethod && Object.keys(privateMethod).forEach(function (key) {
privateMethodObject[key] = privateMethod[key]
})
privateMethodObject['refreshPage'] = function () { this.setData({ refreshPage: !this.data.refreshPage }) }
let actionsObject = {}
!!actionsObject && Object.keys(viewAction).forEach(function (key) {
let action = viewAction[key]
actionsObject[key] = function (...args) {
if (!!args[0] && typeof (args[0].detail) !== 'undefined') {
// 小程序调用
let e = args[0]
let detail = {}
detail = e.detail.hasOwnProperty('value') ? e.detail.value : e.detail
if (e.detail.formId) {
detail.formId = e.detail.formId
}
action.call(this, e.currentTarget.dataset || {}, detail)
} else {
// 自己调用
console.warn(`zachary 抛出,不应把非 action 方法 ${action.name} 放在 action 区域`)
action.call(this, args)
}
}
})
const pageObject = { props, data, ...privateMethodObject, ...actionsObject, ...lifeCycleObject }
Page(pageObject)
}
}
export default new BasePage()