Skip to content

Commit

Permalink
fix(mp-alipay): handleProps before handleRef
Browse files Browse the repository at this point in the history
  • Loading branch information
zhetengbiji committed Oct 24, 2022
1 parent adcb22e commit 8c30bd3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
24 changes: 24 additions & 0 deletions src/platforms/mp-alipay/runtime/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { handleProps } from './wrapper/util'

const MPComponent = Component

function initHook (name, options) {
const oldHook = options[name]
options[name] = function (...args) {
const props = this.props
if (props && props['data-com-type'] === 'wx') {
handleProps(this)
}
if (oldHook) {
return oldHook.apply(this, args)
}
}
}

if (!MPComponent.__$wrappered) {
MPComponent.__$wrappered = true
Component = function (options = {}) {
initHook('onInit', options)
return MPComponent(options)
}
}
29 changes: 13 additions & 16 deletions src/platforms/mp-alipay/runtime/wrapper/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,24 @@ export function initChildVues (mpInstance) {
delete mpInstance._$childVues
}

function handleProps (ref) {
export function handleProps (ref) {
const eventProps = {}
let refProps = ref.props
const eventList = (refProps['data-event-list'] || '').split(',')
// 初始化支付宝小程序组件事件
eventList.forEach(key => {
const handler = refProps[key]
const res = key.match(/^on([A-Z])(\S*)/)
const event = res && (res[1].toLowerCase() + res[2])
refProps[key] = eventProps[key] = function () {
const props = Object.assign({}, refProps)
props[key] = handler
// 由于支付宝事件可能包含多个参数,不使用微信小程序事件格式
delete props['data-com-type']
triggerEvent.bind({ props })(event, {
__args__: [...arguments]
})
}
const handler = refProps[key]
const res = key.match(/^on([A-Z])(\S*)/)
const event = res && (res[1].toLowerCase() + res[2])
refProps[key] = eventProps[key] = function () {
const props = Object.assign({}, refProps)
props[key] = handler
// 由于支付宝事件可能包含多个参数,不使用微信小程序事件格式
delete props['data-com-type']
triggerEvent.bind({ props })(event, {
__args__: [...arguments]
})
}
})
// 处理 props 重写
Object.defineProperty(ref, 'props', {
Expand All @@ -142,9 +142,6 @@ export function handleRef (ref) {
if (!(ref && this.$vm)) {
return
}
if (ref.props['data-com-type'] === 'wx') {
handleProps(ref)
}
const refName = ref.props['data-ref']
const refInForName = ref.props['data-ref-in-for']
if (refName) {
Expand Down

0 comments on commit 8c30bd3

Please sign in to comment.