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.
45 lines
925 B
45 lines
925 B
|
3 years ago
|
import {
|
||
|
|
cached,
|
||
|
|
camelize
|
||
|
|
} from 'uni-shared'
|
||
|
|
|
||
|
|
const MPPage = Page
|
||
|
|
const MPComponent = Component
|
||
|
|
|
||
|
|
const customizeRE = /:/g
|
||
|
|
|
||
|
|
const customize = cached((str) => {
|
||
|
|
return camelize(str.replace(customizeRE, '-'))
|
||
|
|
})
|
||
|
|
|
||
|
|
function initTriggerEvent (mpInstance) {
|
||
|
|
const oldTriggerEvent = mpInstance.triggerEvent
|
||
|
|
mpInstance.triggerEvent = function (event, ...args) {
|
||
|
|
return oldTriggerEvent.apply(mpInstance, [customize(event), ...args])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function initHook (name, options) {
|
||
|
|
const oldHook = options[name]
|
||
|
|
if (!oldHook) {
|
||
|
|
options[name] = function () {
|
||
|
|
initTriggerEvent(this)
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
options[name] = function (...args) {
|
||
|
|
initTriggerEvent(this)
|
||
|
|
return oldHook.apply(this, args)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Page = function (options = {}) {
|
||
|
|
initHook('onLoad', options)
|
||
|
|
return MPPage(options)
|
||
|
|
}
|
||
|
|
|
||
|
|
Component = function (options = {}) {
|
||
|
|
initHook('created', options)
|
||
|
|
return MPComponent(options)
|
||
|
|
}
|