Skip to content

Commit

Permalink
'组件中有子组件是列表时,初始值为空会报错的解决'
Browse files Browse the repository at this point in the history
  • Loading branch information
useryu committed Sep 28, 2017
1 parent 9d6922e commit ae834b0
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions packages/wepy/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -252,9 +261,6 @@ export default class {
* 对于在repeat中的组件,index改变时需要修改对应的数据
*/
$setIndex (index) {
if (this.$index === index)
return;

this.$index = index;

let props = this.props,
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -483,4 +503,4 @@ function getEventsFn (comContext, evtName) {
fnFn = fn;
}
return fnFn;
}
}

0 comments on commit ae834b0

Please sign in to comment.