diff --git a/mptemp/commonwxs.wxs b/mptemp/commonwxs.wxs index 67fb035..f7abfb2 100644 --- a/mptemp/commonwxs.wxs +++ b/mptemp/commonwxs.wxs @@ -40,25 +40,12 @@ var lite = function(v) { return v.constructor === 'Number' || v.constructor === 'String' } -var isArray = function(v) { - return v.constructor === 'Array' -} - -var compact = function(v) { - return v.filter(function(item) { - return item !== null - }) -} module.exports = { getFinalStyle: getFinalStyle, lite: lite, - isArray: isArray, - compact: compact, s: getFinalStyle, l: lite, - a: isArray, - c: compact }; \ No newline at end of file diff --git a/packages/wx-react-native/miniprogram_dist/component/WXFlatList/index.wxml b/packages/wx-react-native/miniprogram_dist/component/WXFlatList/index.wxml index cfebcfe..854618f 100644 --- a/packages/wx-react-native/miniprogram_dist/component/WXFlatList/index.wxml +++ b/packages/wx-react-native/miniprogram_dist/component/WXFlatList/index.wxml @@ -61,7 +61,7 @@ - + - + - + - + !nkeysSet.has(key)) +} + function getObjectPathInner(v, prefix, result) { const tv = typeof v @@ -54,17 +60,17 @@ function getChangePathInner(newR, oldR, prefix, result) { ) { getObjectPathInner(newR, prefix, result) } else if (Array.isArray(newR)) { + // 由于小程序 setData 设置为 undefined 会出问题。 所以这种情况直接设置对象 + if (newR.length < oldR.length) { + result[prefix] = newR + return + } + for (let i = 0; i < newR.length; i++) { const v = newR[i] const ov = oldR[i] getChangePathInner(v, ov, `${prefix}[${i}]`, result) } - - if (newR.length < oldR.length) { - for (let i = newR.length; i < oldR.length; i ++) { - result[`${prefix}[${i}]`] = null - } - } } else if (tn === 'object' && tn !== null) { if (newR.__isAnimation__) { result[prefix] = newR @@ -73,18 +79,21 @@ function getChangePathInner(newR, oldR, prefix, result) { const nkeys = Object.keys(newR) + const okeys = Object.keys(oldR) + + // 由于小程序 setData 设置为 undefined 会出问题。 所以这种情况直接设置对象 + // TODO 这种情况下, 是否依然可以减少数据的传递呢?? + if (hasMoreKeys(okeys, nkeys)) { + result[prefix] = newR + return + } + for (let i = 0; i < nkeys.length; i++) { const k = nkeys[i] const v = newR[k] const ov = oldR[k] getChangePathInner(v, ov, `${prefix}.${k}`, result) } - - const okeys = Object.keys(oldR) - const onlyNkeys = okeys.filter(k => newR[k] === undefined) - for(let i = 0; i < onlyNkeys.length; i ++) { - result[`${prefix}.${onlyNkeys[i]}`] = null - } } else { result[prefix] = newR } diff --git a/src/tran/childrenToTemplate.js b/src/tran/childrenToTemplate.js index 2e150e2..da99221 100644 --- a/src/tran/childrenToTemplate.js +++ b/src/tran/childrenToTemplate.js @@ -117,7 +117,7 @@ export default function childrenToTemplate(ast, info) { [ t.jsxAttribute(t.jsxIdentifier('datakey'), t.stringLiteral(datakey)), t.jsxAttribute(t.jsxIdentifier('tempVnode'), ele), - t.jsxAttribute(t.jsxIdentifier('wx:if'), t.stringLiteral(`{{${datakey}}}`)), + t.jsxAttribute(t.jsxIdentifier('wx:if'), t.stringLiteral(`{{${datakey}}} !== undefined`)), t.jsxAttribute(t.jsxIdentifier('is'), t.stringLiteral(tempName)), t.jsxAttribute(t.jsxIdentifier('data'), t.stringLiteral(`{{d: ${datakey}}}`)) ] diff --git a/src/tran/geneWxml.js b/src/tran/geneWxml.js index b467fab..5f7f6b6 100644 --- a/src/tran/geneWxml.js +++ b/src/tran/geneWxml.js @@ -66,7 +66,16 @@ export default function(info) { const name = childTemplates[i]; // 如果只使用一个child 小程序会报递归, 然后就不渲染了 const subT = ` - +