提交 7834536e 编写于 作者: fxy060608's avatar fxy060608

fix(mp): dynamic event

上级 abd2cbb2
const migrate = require('../lib/index')
// const migrate = require('../lib/index')
migrate('/Users/fxy/Downloads/wa-vantui_1.1')
// migrate('/Users/fxy/Downloads/wa-vantui_1.1')
// migrate('/Users/fxy/Downloads/wa-vantui_1.1/pages')
// const {
// parse
// } = require('mustache')
// console.log(parse("van-notice-bar__content {{ !scrollable && !wrapable ? 'van-ellipsis' : '' }}"))
const {
parse
} = require('mustache')
function parseMustache(expr, identifier = false) {
if (!expr) {
return ''
}
const tokens = parse(expr)
const isIdentifier = tokens.length === 1
return tokens.map(token => {
if (token[0] === 'text') {
if (identifier) {
return token[1]
}
return `'${token[1]}'`
} else if (token[0] === '!') { // {{ !loading }}
return `(!${token[1]})`
} else if (token[0] === 'name') {
if (isIdentifier) {
return token[1]
}
return `(${token[1]})`
}
}).join('+')
}
console.log(parseMustache("click ",true)==='click ')
// const {
// transformTemplate
......
......@@ -9,11 +9,11 @@ describe('wxml:compiler', () => {
it('generate event', () => {
assertCodegen(
`<view bindtouchstart="startDrag" catchtouchmove="{{ catchMove ? 'noop' : '' }}"/>`,
`<uni-shadow-root><view @touchstart="startDrag" @touchmove.stop.prevent="catchMove ? 'noop' : ''"></view></uni-shadow-root>`
`<uni-shadow-root><view @touchstart="startDrag" @touchmove.stop.prevent="_$self[(catchMove ? 'noop' : '')||'_$noop']($event)"></view></uni-shadow-root>`
)
assertCodegen(
`<uni-transition bind:click="click" bindtouchstart="startDrag" catchtouchmove="{{ catchMove ? 'noop' : '' }}"/>`,
`<uni-shadow-root><uni-transition @click="click" @touchstart.native="startDrag" @touchmove.native.stop.prevent="catchMove ? 'noop' : ''"></uni-transition></uni-shadow-root>`
`<uni-shadow-root><uni-transition @click="click" @touchstart.native="startDrag" @touchmove.native.stop.prevent="_$self[(catchMove ? 'noop' : '')||'_$noop']($event)"></uni-transition></uni-shadow-root>`
)
})
it('generate class', () => {
......
......@@ -127,7 +127,7 @@ function transformFor(attribs) {
if (vKey) {
if (vKey === '*this') {
vKey = vItem
} else if (vKey !== vItem && vKey.indexOf('.')===-1) {// wx:for-key="{{item.value}}"
} else if (vKey !== vItem && vKey.indexOf('.') === -1) { // wx:for-key="{{item.value}}"
vKey = vItem + '.' + vKey
}
attribs[':key'] = vKey
......@@ -163,7 +163,11 @@ function transformEvent(name, value, attribs, state) {
event = transformEventName(name.replace(captureCatchRE, ''), state) + '.stop.prevent.capture'
}
if (event !== name) {
attribs[event] = parseMustache(value, true)
let newValue = parseMustache(value, true)
if (newValue !== value) {
newValue = `_$self[(${newValue})||'_$noop']($event)`
}
attribs[event] = newValue
return true
}
}
......
......@@ -758,6 +758,9 @@ var polyfill = {
beforeCreate () {
// 取消 development 时的 Proxy,避免小程序组件模板中使用尚未定义的属性告警
this._renderProxy = this;
this._$self = this;
this._$noop = noop;
},
created () { // properties 中可能会访问 methods,故需要在 created 中初始化
initState(this);
......
import {
noop
} from 'uni-shared'
import {
initState
} from './state/index'
......@@ -19,6 +23,9 @@ export default {
beforeCreate () {
// 取消 development 时的 Proxy,避免小程序组件模板中使用尚未定义的属性告警
this._renderProxy = this
this._$self = this
this._$noop = noop
},
created () { // properties 中可能会访问 methods,故需要在 created 中初始化
initState(this)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册