提交 5518728d 编写于 作者: fxy060608's avatar fxy060608

feat(v3): v-model

上级 62fb0d52
......@@ -8598,19 +8598,8 @@ var serviceContext = (function () {
}
});
function optimize (k, v) {
if (
k === V_IF ||
k === V_ELSE_IF ||
k === V_SHOW
) {
return v ? 1 : 0
}
return v
}
function publishHandler (eventType, args, pageIds) {
args = JSON.stringify(args, optimize);
args = JSON.stringify(args);
if (process.env.NODE_ENV !== 'production') {
console.log(`UNIAPP[publishHandler]:[${+new Date()}]`, eventType, args, pageIds);
}
......@@ -9158,17 +9147,17 @@ var serviceContext = (function () {
return result
}
function initData (Vue) {
function initData(Vue) {
Vue.prototype._$s = setData;
Vue.prototype._$i = setIfData;
Vue.prototype._$f = setForData;
Vue.prototype._$e = setElseIfData;
Vue.prototype._$setData = function setData (type, data) {
Vue.prototype._$setData = function setData(type, data) {
this._$vd.push(type, this._$id, data);
};
Vue.prototype._$mounted = function mounted () {
Vue.prototype._$mounted = function mounted() {
if (!this._$vd) {
return
}
......@@ -9181,7 +9170,7 @@ var serviceContext = (function () {
}
};
Vue.prototype._$updated = function updated () {
Vue.prototype._$updated = function updated() {
if (!this._$vd) {
return
}
......@@ -9199,13 +9188,13 @@ var serviceContext = (function () {
};
Object.defineProperty(Vue.prototype, '_$vd', {
get () {
get() {
return this.$root._$vdomSync
}
});
Vue.mixin({
beforeCreate () {
beforeCreate() {
if (this.$options.mpType) {
this.mpType = this.$options.mpType;
}
......@@ -9225,7 +9214,7 @@ var serviceContext = (function () {
this._$newData = Object.create(null);
}
},
beforeUpdate () {
beforeUpdate() {
if (!this._$vd) {
return
}
......@@ -9234,7 +9223,7 @@ var serviceContext = (function () {
console.log(`[${this._$id}] beforeUpdate ` + Date.now());
this._$newData = Object.create(null);
},
beforeDestroy () {
beforeDestroy() {
if (!this._$vd) {
return
}
......@@ -9244,7 +9233,7 @@ var serviceContext = (function () {
});
}
function setData (id, name, value) {
function setData(id, name, value) {
switch (name) {
case B_CLASS:
value = this._$stringifyClass(value);
......@@ -9252,9 +9241,10 @@ var serviceContext = (function () {
case B_STYLE:
value = this._$normalizeStyleBinding(value);
break
case V_IF:
case V_IF:
case V_SHOW:
case V_ELSE_IF:
value = !!value;
value = value ? 1 : 0;
break
case V_FOR:
return setForData.call(this, id, value)
......@@ -9263,7 +9253,7 @@ var serviceContext = (function () {
return ((this._$newData[id] || (this._$newData[id] = {}))[name] = value)
}
function setForData (id, value) {
function setForData(id, value) {
const diffData = this._$newData[id] || (this._$newData[id] = {});
const vForData = diffData[V_FOR] || (diffData[V_FOR] = []);
......@@ -9284,11 +9274,11 @@ var serviceContext = (function () {
return key
}
function setIfData (id, value) {
function setIfData(id, value) {
return ((this._$newData[id] || (this._$newData[id] = {}))[V_IF] = !!value)
}
function setElseIfData (id, value) {
function setElseIfData(id, value) {
return ((this._$newData[id] || (this._$newData[id] = {}))[V_ELSE_IF] = !!value)
}
......
......@@ -2,6 +2,8 @@ const compiler = require('../lib')
const res = compiler.compile(
`
<my-component v-model="\n test \n" />
`, {
resourcePath: '/User/fxy/Documents/test.wxml',
isReservedTag: function (tag) {
......@@ -19,4 +21,4 @@ const res = compiler.compile(
console.log(require('util').inspect(res, {
colors: true,
depth: null
}))
}))
......@@ -32,10 +32,12 @@ function parseBinding (el, genVar) {
el.styleBinding && (el.styleBinding = genVar('s', el.styleBinding))
}
function parseDirs (el, genVar) {
function parseDirs (el, genVar, ignoreDirs = []) {
el.directives && el.directives.forEach(dir => {
dir.value && (dir.value = genVar('v-' + dir.name, dir.value))
dir.isDynamicArg && (dir.arg = genVar('v-' + dir.name + '-arg', dir.arg))
if (ignoreDirs.indexOf(dir.name) === -1) {
dir.value && (dir.value = genVar('v-' + dir.name, dir.value))
dir.isDynamicArg && (dir.arg = genVar('v-' + dir.name + '-arg', dir.arg))
}
})
}
......
const {
ID,
hasOwn,
addAttr
addRawAttr
} = require('./util')
module.exports = function preTransformNode (el, options) {
module.exports = function preTransformNode (el, options) {
if (!hasOwn(options, 'nid')) {
options.nid = 0
}
addAttr(el, ID, options.nid++)
addRawAttr(el, ID, options.nid++)
if (el.attrsMap['v-for']) {
el.forId = el.attrsMap[ID]
}
......
......@@ -26,7 +26,7 @@ const {
const parseEvent = require('./parser/event-parser')
const parseBlock = require('./parser/block-parser')
const basePreTransformNode = require('./pre-transform-node')
const preTransformNode = require('./pre-transform-node')
const optimize = require('./optimizer')
......@@ -87,7 +87,7 @@ function transformNode (el, parent, state) {
parseIf(el, createGenVar)
parseBinding(el, genVar)
parseDirs(el, genVar)
parseDirs(el, genVar, ['model'])
if (!isComponent(el.tag)) {
parseAttrs(el, genVar)
......@@ -106,27 +106,30 @@ function postTransformNode (el, options) {
}
}
function parseTag (el) {
if (el.tag === 'input' || el.tag === 'textarea') {
el.tag = `c-${el.tag.substr(0, 1)}` // 返回一个自定义组件标签,保证 v-model
function genVModel (el) {
if (
(el.tag === 'input' || el.tag === 'textarea') &&
el.directives &&
el.directives.find(dir => dir.name === 'model')
) {
const prop = el.props.find(prop => prop.name === 'value')
prop.value = createGenVar(el.attrsMap[ID])('v-model', prop.value)
}
if (el.model) {
el.model.value = createGenVar(el.attrsMap[ID])('v-model', el.model.value)
}
}
function genData (el) {
delete el.$parentIterator3
if (el.model) {
el.model.callback = `function ($$v) {}`
}
genVModel(el)
return ''
}
module.exports = {
preTransformNode: function (el, options) {
parseTag(el)
return basePreTransformNode(el, options)
},
preTransformNode,
postTransformNode,
genData
}
......@@ -28,7 +28,7 @@ function isVar (str) {
return true
}
function addAttr (el, name, value) {
function addRawAttr (el, name, value) {
el.attrsMap[name] = value
el.attrsList.push({
name,
......@@ -42,7 +42,7 @@ function updateEleId (el, it, state) {
}
const id = el.attrsMap[ID]
const newId = Number.isInteger(id) ? `("${id}-"+${it})` : `(${id}+${it})`
addAttr(el, ID, newId)
addRawAttr(el, ID, newId)
const attr = el.attrs.find(attr => attr.name === ID)
attr.value = newId
el.children.forEach(child => {
......@@ -121,7 +121,7 @@ function processForKey (el) {
const keyIndex = forEl.children.indexOf(el)
el.key = `${forEl.forId}+'-${keyIndex}'+${it}`
} else { // 当 template 下只有文本节点
if (el.children && el.children.length && !el.children.find(child => child.key)) {
if (el.children && el.children.length && !el.children.find(child => child.key)) {
el.children[0].parent = el
el.children[0].key = `${forEl.forId}+'-0'+${it}`
return true
......@@ -146,17 +146,63 @@ function traverseNode (el, parent, state) {
el.scopedSlots && Object.values(el.scopedSlots).forEach(slot => traverseNode(slot, el, state))
}
function addAttr (el, name, value, dynamic) {
const attrs = dynamic
? (el.dynamicAttrs || (el.dynamicAttrs = []))
: (el.attrs || (el.attrs = []))
attrs.push({
name,
value,
dynamic
})
el.plain = false
}
function removeRawAttr (el, name) {
delete el.attrsMap[name]
const index = el.attrsList.findIndex(attr => attr.name === name)
index !== -1 && el.attrsList.splice(index, 1)
}
function removeRawBindingAttr (el, name) {
removeRawAttr(el, ':' + name)
removeRawAttr(el, 'v-bind:' + name)
console.log(el)
throw new Error('123')
}
function addHandler (el, name, value, important) {
const events = el.events || (el.events = {})
const handlers = events[name]
const newHandler = {
value: value.trim(),
dynamic: undefined
}
if (Array.isArray(handlers)) {
important ? handlers.unshift(newHandler) : handlers.push(newHandler)
} else if (handlers) {
events[name] = important ? [newHandler, handlers] : [handlers, newHandler]
} else {
events[name] = newHandler
}
el.plain = false
}
module.exports = {
V_FOR,
V_IF,
V_ELSE_IF,
ID,
ID,
SET_DATA,
GET_DATA,
isVar,
hasOwn,
addAttr,
addRawAttr,
removeRawAttr,
removeRawBindingAttr,
getForEl,
addHandler,
processForKey,
updateForEleId,
getBindingAttr,
......
......@@ -183,12 +183,7 @@ const {
} = require('./h5')
function isComponent (tagName) {
if (
tagName === 'block' ||
tagName === 'template' ||
tagName === 'c-i' || // v3 service input => c-i
tagName === 'c-t' // v3 service textarea => c-t
) {
if (tagName === 'block' || tagName === 'template') {
return false
}
return !hasOwn(tags, getTagName(tagName.replace('v-uni-', '')))
......
......@@ -3696,6 +3696,10 @@
return
}
if(process.env.UNI_PLATFORM !== 'h5'){ // fixed by xxxxxx 非 h5 平台 type 不会是 checkbox,radio
return
}
var typeBinding;
if (map[':type'] || map['v-bind:type']) {
typeBinding = getBindingAttr(el, 'type');
......
......@@ -3318,6 +3318,10 @@ function preTransformNode (el, options) {
return
}
if(process.env.UNI_PLATFORM !== 'h5'){ // fixed by xxxxxx 非 h5 平台 type 不会是 checkbox,radio
return
}
var typeBinding;
if (map[':type'] || map['v-bind:type']) {
typeBinding = getBindingAttr(el, 'type');
......
......@@ -213,7 +213,7 @@ module.exports = function (pagesJson, userManifestJson) {
let flexDir = false
if (manifestJson.plus.nvueCompiler && manifestJson.plus.nvueCompiler === 'uni-app') {
if (process.env.UNI_USING_NVUE_COMPILER) {
appJson.nvueCompiler = 'uni-app'
flexDir = getFlexDirection(manifestJson.plus)
} else {
......
......@@ -14,7 +14,8 @@ import {
import {
V_IF,
V_FOR,
V_FOR,
V_SHOW,
V_ELSE_IF,
B_CLASS,
B_STYLE
......@@ -110,7 +111,7 @@ export function initData (Vue) {
})
}
function setData (id, name, value) {
function setData (id, name, value) {
switch (name) {
case B_CLASS:
value = this._$stringifyClass(value)
......@@ -118,9 +119,10 @@ function setData (id, name, value) {
case B_STYLE:
value = this._$normalizeStyleBinding(value)
break
case V_IF:
case V_IF:
case V_SHOW:
case V_ELSE_IF:
value = !!value
value = value ? 1 : 0
break
case V_FOR:
return setForData.call(this, id, value)
......
import {
V_IF,
V_ELSE_IF,
V_SHOW
}
from './constants'
function optimize (k, v) {
if (
k === V_IF ||
k === V_ELSE_IF ||
k === V_SHOW
) {
return v ? 1 : 0
}
return v
}
export function publishHandler (eventType, args, pageIds) {
args = JSON.stringify(args, optimize)
args = JSON.stringify(args)
if (process.env.NODE_ENV !== 'production') {
console.log(`UNIAPP[publishHandler]:[${+new Date()}]`, eventType, args, pageIds)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册