From ae834b0fc9c24cf183e31b3e9817bfcac848bc03 Mon Sep 17 00:00:00 2001 From: "fisher.yu" Date: Thu, 28 Sep 2017 10:41:24 +0800 Subject: [PATCH] =?UTF-8?q?'=E7=BB=84=E4=BB=B6=E4=B8=AD=E6=9C=89=E5=AD=90?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=98=AF=E5=88=97=E8=A1=A8=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=80=BC=E4=B8=BA=E7=A9=BA=E4=BC=9A=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E8=A7=A3=E5=86=B3'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/wepy/src/component.js | 58 +++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/packages/wepy/src/component.js b/packages/wepy/src/component.js index e7c43361b..54cf47afd 100644 --- a/packages/wepy/src/component.js +++ b/packages/wepy/src/component.js @@ -134,15 +134,24 @@ export default class { if (binded) { if (typeof(binded) === 'object') { props[key].repeat = binded.for; + props[key].item = binded.item; + props[key].index = binded.index; + props[key].key = binded.key; + props[key].value = binded.value; inRepeat = true; let bindfor = binded.for, binddata = $parent; bindfor.split('.').forEach(t => { - binddata = binddata[t]; + if (binddata[t]){ + binddata = binddata[t]; + }else{ + binddata = [{}]; + } }); - repeatKey = Object.keys(binddata)[0]; - + if (binddata && (typeof binddata === 'object' || typeof binddata === 'string')) { + repeatKey = Object.keys(binddata)[0]; + } if (!this.$mappingProps[key]) this.$mappingProps[key] = {}; this.$mappingProps[key]['parent'] = { @@ -174,7 +183,7 @@ export default class { } this.$data = util.$copy(this.data, true); - if (inRepeat) + if (inRepeat && repeatKey !== undefined) this.$setIndex(repeatKey); if (this.computed) { @@ -252,9 +261,6 @@ export default class { * 对于在repeat中的组件,index改变时需要修改对应的数据 */ $setIndex (index) { - if (this.$index === index) - return; - this.$index = index; let props = this.props, @@ -270,10 +276,24 @@ export default class { if (typeof(binded) === 'object') { let bindfor = binded.for, binddata = $parent; bindfor.split('.').forEach(t => { - binddata = binddata[t]; + if (binddata[t]){ + binddata = binddata[t]; + }else{ + binddata = [{}]; + } }); - val = binddata[index]; + index = Array.isArray(binddata) ? +index : index; + + if (props[key].value === props[key].item) { + val = binddata[index]; + } else if (props[key].value === props[key].index) { + val = index; + } else if (props[key].value === props[key].key) { + val = index; + } else { + val = $parent[props[key].value]; + } this.$index = index; this.data[key] = val; this[key] = val; @@ -417,6 +437,15 @@ export default class { this.$$phase = '$digest'; while (this.$$phase) { let readyToSet = {}; + if (this.computed) { + for (k in this.computed) { // If there are computed property, calculated every times + let fn = this.computed[k], val = fn.call(this); + if (!util.$isEqual(this[k], val)) { // Value changed, then send to ReadyToSet + readyToSet[this.$prefix + k] = val; + this[k] = util.$copy(val, true); + } + } + } for (k in originData) { if (!util.$isEqual(this[k], originData[k])) { // compare if new data is equal to original data // data watch trigger @@ -452,15 +481,6 @@ export default class { } } if (Object.keys(readyToSet).length) { - if (this.computed) { - for (k in this.computed) { // If there are computed property, calculated every times - let fn = this.computed[k], val = fn.call(this); - if (!util.$isEqual(this[k], val)) { // Value changed, then send to ReadyToSet - readyToSet[this.$prefix + k] = val; - this[k] = util.$copy(val, true); - } - } - } this.setData(readyToSet); } this.$$phase = (this.$$phase === '$apply') ? '$digest' : false; @@ -483,4 +503,4 @@ function getEventsFn (comContext, evtName) { fnFn = fn; } return fnFn; -} \ No newline at end of file +}