Skip to content

Commit

Permalink
fix: 区分事件调用时的scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Simbachen committed Jul 19, 2018
1 parent 8dbc00d commit f717ccb
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/taro-weapp/src/create-component.js
Expand Up @@ -29,8 +29,8 @@ function processEvent (eventHandlerName, obj) {
Object.assign(event.currentTarget, event.detail)
}

let scope = this.$component

const scope = this.$component
let callScope = scope
const isCustomEvt = event.detail && event.detail.__isCustomEvt === true
const isAnonymousFn = eventHandlerName.indexOf(anonymousFnNamePreffix) > -1
let realArgs = []
Expand All @@ -55,7 +55,7 @@ function processEvent (eventHandlerName, obj) {
// 普通的事件(非匿名函数),会直接call
if (!isAnonymousFn) {
if ('so' in bindArgs && bindArgs['so'] !== 'this') {
scope = bindArgs['so']
callScope = bindArgs['so']
delete bindArgs['so']
}
if(!isEmptyObject(bindArgs)) {
Expand Down Expand Up @@ -84,19 +84,17 @@ function processEvent (eventHandlerName, obj) {
// 如果不是匿名函数,则将scope从参数中取出来,执行。
// 否则继续作为参数传递下去
if (!isAnonymousFn && realArgs.length > 0) {
realArgs[0] && (scope = realArgs[0])
realArgs[0] && (callScope = realArgs[0])
realArgs.shift()
}
}

// 如果是匿名函数,scope指向自己,并且将传入的scope作为第一个参数传递下去

if (realArgs.length > 0) {
scope[eventHandlerName].apply(scope, realArgs)
} else if (!isCustomEvt) {
scope[eventHandlerName].call(scope, event)
scope[eventHandlerName].apply(callScope, realArgs)
} else {
scope[eventHandlerName].call(scope)
scope[eventHandlerName].call(callScope)
}
}
}
Expand Down

0 comments on commit f717ccb

Please sign in to comment.