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 ae834b0 commit e75e874
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions packages/wepy/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ 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;

Expand All @@ -149,9 +145,8 @@ export default class {
binddata = [{}];
}
});
if (binddata && (typeof binddata === 'object' || typeof binddata === 'string')) {
repeatKey = Object.keys(binddata)[0];
}
repeatKey = Object.keys(binddata)[0];


if (!this.$mappingProps[key]) this.$mappingProps[key] = {};
this.$mappingProps[key]['parent'] = {
Expand Down Expand Up @@ -183,7 +178,7 @@ export default class {
}

this.$data = util.$copy(this.data, true);
if (inRepeat && repeatKey !== undefined)
if (inRepeat)
this.$setIndex(repeatKey);

if (this.computed) {
Expand Down Expand Up @@ -261,6 +256,9 @@ export default class {
* 对于在repeat中的组件,index改变时需要修改对应的数据
*/
$setIndex (index) {
if (this.$index === index)
return;

this.$index = index;

let props = this.props,
Expand All @@ -283,17 +281,7 @@ export default class {
}
});

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];
}
val = binddata[index];
this.$index = index;
this.data[key] = val;
this[key] = val;
Expand Down Expand Up @@ -437,15 +425,6 @@ 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 @@ -481,6 +460,15 @@ 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 @@ -503,4 +491,4 @@ function getEventsFn (comContext, evtName) {
fnFn = fn;
}
return fnFn;
}
}

0 comments on commit e75e874

Please sign in to comment.