Skip to content

Commit

Permalink
fix(alita wx-react wx-react-native): 修复tempalte是数组的情况,在数据变少的时候bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ykforerlang committed Jul 25, 2019
1 parent e2b88ce commit 8326a20
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 47 deletions.
8 changes: 8 additions & 0 deletions mptemp/commonwxs.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ 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
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<wxs src="../commonwxs.wxs" module="tools" />
<wxs src="../../../../../commonwxs.wxs" module="tools" />
<wxs module="methods">
var ArrayContains=function(arr,ele){
var find=false
Expand Down Expand Up @@ -59,7 +59,7 @@

<block wx:if="{{_r.numColumns >= 2}}">
<view style="display: flex; flex-direction: row; justify-content: flex-start; flex-wrap: wrap">
<view wx:for="{{_r.renderItemData}}" wx:key="key" style="width: {{100 / r.numColumns}}%">
<view wx:for="{{tools.compact(_r.renderItemData)}}" wx:key="key" style="width: {{100 / r.numColumns}}%">
<renderItemCPT id="id_{{index}}"
diuu="{{item.renderItemDIUU}}"
_r="{{item.renderItemDIUUR}}"
Expand All @@ -68,7 +68,7 @@
</view>
</block>
<block wx:else>
<block wx:for="{{_r.renderItemData}}" wx:key="key">
<block wx:for="{{tools.compact(_r.renderItemData)}}" wx:key="key">
<view wx:if="{{methods.ArrayContains(_r.stickyHeaderIndices,index)}}" style="{{sti['stickyContainerStyle'+index]}}">
<renderItemCPT id="id_{{index}}"
diuu="{{item.renderItemDIUU}}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<wxs src="../commonwxs.wxs" module="tools"/>
<wxs src="../../../../../commonwxs.wxs" module="tools" />
<picker-view
indicator-style="height: 50px"
style="position: absolute; height: {{_r.itemLength * 50}}px; width: 100%"
value="{{_r.seleIndex}}"
bindchange="onValueChange">
<picker-view-column>
<block
wx:for="{{_r.childrenData}}"
wx:for="{{tools.compact(_r.childrenData)}}"
wx:key="key"
>
<childrenCPT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<wxs src="../commonwxs.wxs" module="tools" />
<wxs src="../../../../../commonwxs.wxs" module="tools" />
<scroll-view
bindscrolltoupper="onScrollToupper"
scroll-y
Expand Down Expand Up @@ -32,15 +32,15 @@
style="{{tools.getFinalStyle(_r.ListHeaderComponentDIUUstyle)}}"
/>

<block wx:for="{{_r.sectionsData}}" wx:key="key">
<block wx:for="{{tools.compact(_r.sectionsData)}}" wx:key="key">
<renderSectionHeaderCPT
wx:if="{{item.renderSectionHeaderDIUU}}"
diuu="{{item.renderSectionHeaderDIUU}}"
_r="{{item.renderSectionHeaderDIUUR}}"
style="{{tools.getFinalStyle(item.renderSectionHeaderDIUUstyle)}}"
/>

<block wx:for="{{item.renderItemData}}" wx:key="key">
<block wx:for="{{tools.compact(item.renderItemData)}}" wx:key="key">
<renderItemCPT
diuu="{{item.renderItemDIUU}}"
_r="{{item.renderItemDIUUR}}"
Expand Down
17 changes: 0 additions & 17 deletions packages/wx-react-native/miniprogram_dist/component/commonwxs.wxs

This file was deleted.

33 changes: 12 additions & 21 deletions packages/wx-react/miniprogram_dist/getChangePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
* LICENSE file in the root directory of this source tree.
*
*/

function hasMoreKeys(okeys, nkeys) {
const nkeysSet = new Set(nkeys)

return okeys.some(key => !nkeysSet.has(key))
}

function getObjectPathInner(v, prefix, result) {
const tv = typeof v
Expand Down Expand Up @@ -60,17 +54,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
Expand All @@ -79,21 +73,18 @@ 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
}
Expand Down
2 changes: 1 addition & 1 deletion src/tran/geneWxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function(info) {
const name = childTemplates[i];
// 如果只使用一个child 小程序会报递归, 然后就不渲染了
const subT = `
<template name="${name}"><block wx:if="{{t.a(d)}}"><block wx:for="{{d}}" wx:key="key"><block wx:if="{{t.l(item)}}">{{item}}</block><template wx:else is="{{item.tempName}}" data="{{...item}}"></template></block></block><template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"></template></template>
<template name="${name}"><block wx:if="{{t.a(d)}}"><block wx:for="{{t.c(d)}}" wx:key="key"><block wx:if="{{t.l(item)}}">{{item}}</block><template wx:else is="{{item.tempName}}" data="{{...item}}"></template></block></block><template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"></template></template>
`;

templateWxml = subT + templateWxml;
Expand Down

0 comments on commit 8326a20

Please sign in to comment.