riku
2022-10-28 b45a01a8bee4a9bff5f9c248ead301b8675d1099
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
/**
 * mixins 为 Page 增加 mixin 功能
 * 来源:https://segmentfault.com/a/1190000019527762
 */
const originPage = Page;
const originProperties = ['data', 'properties', 'options'];
const originMethods = ['onLoad', 'onReady', 'onShow', 'onHide', 'onUnload', 'onPullDownRefresh', 'onReachBottom', 'onShareAppMessage', 'onPageScroll', 'onTabItemTap'];
 
function merge (mixins, options) {
    mixins.forEach((mixin) => {
        if (Object.prototype.toString.call(mixin) !== '[object Object]') {
            throw new Error('mixin 类型必须为对象!')
        }
        for (let [key, value] of Object.entries(mixin)) {
            if (originProperties.includes(key)) {
                options[key] = { ...value, ...options[key] }
            } else if (originMethods.includes(key)) {
                const originFunc = options[key];
                options[key] = function (...args) {
                    value.call(this, ...args);
                    return originFunc && originFunc.call(this, ...args)
                }
            } else {
                options = { ...mixin, ...options }
            }
        }
    });
    return options
}
 
Page = (options) => {
    const mixins = options.mixins;
    if (Array.isArray(mixins)) {
        delete options.mixins;
        options = merge(mixins, options)
    }
    originPage(options)
};