Skip to content

Commit

Permalink
added - #712
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Jan 2, 2018
1 parent 919de65 commit 216c45b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
40 changes: 39 additions & 1 deletion packages/wepy/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ export default class {
let noPrefix = t.replace(reg, '');
this.$data[noPrefix] = util.$copy(k[t], true);
}

// In the same page redirection, $wxpage does not update, so use the page $wxpage
if (typeof v === 'function') {
return this.$root.$wxpage.setData(k, v);
}
return this.$root.$wxpage.setData(k);
}

Expand Down Expand Up @@ -555,11 +559,45 @@ export default class {
}
}
if (Object.keys(readyToSet).length) {
this.setData(readyToSet);
this.setData(readyToSet, () => {
if (this.$$nextTick) {
if (this.$$nextTick.promise) {
this.$$nextTick();
} else {
this.$$nextTick.call(this);
}
}
this.$$nextTick = null;
});
} else {
if (this.$$nextTick) {
if (this.$$nextTick.promise) {
this.$$nextTick();
} else {
this.$$nextTick.call(this);
}
}
this.$$nextTick = null;
}
this.$$phase = (this.$$phase === '$apply') ? '$digest' : false;
}
}
/**
* setData callback
* @param {Function} callback function, if fn is undefined, then return a promise
* @return {Promsie} if fn is undefined, then return a promise, otherwise return undefiend
*/
$nextTick (fn) {
if (typeof fn === 'undefined') {
return new Promise((resolve, reject) => {
this.$$nextTick = function () {
resolve();
};
this.$$nextTick.promise = true;
});
}
this.$$nextTick = fn;
}

}

Expand Down
52 changes: 52 additions & 0 deletions packages/wepy/test/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,58 @@ describe('component.js', () => {

});

it('$nextTick', function (done) {
//this.timeout(1500);
com.a = +new Date();
com.$nextTick(function () {
assert.strictEqual(this, com, 'setData callback use this');
setTimeout(function () {
assert.strictEqual(com.$$nextTick, null, '$$nextTick should be cleared');
done();
}, 100);
});
com.$apply();
});

it('$nextTick using promise', function (done) {
//this.timeout(1500);
com.a = +new Date();
com.$nextTick().then(function () {
setTimeout(function () {
assert.strictEqual(com.$$nextTick, null, '$$nextTick should be cleared');
done();
}, 100);
});
com.$apply();
});

it('$nextTick callback for clear data', function (done) {
//this.timeout(1500);
com.a = +new Date();
com.$apply();
com.$nextTick(function () {
assert.strictEqual(this, com, 'setData callback use this');
setTimeout(function () {
assert.strictEqual(com.$$nextTick, null, '$$nextTick should be cleared');
done();
}, 100);
});
com.$apply();
});

it('$nextTick using promise for clear data', function (done) {
//this.timeout(1500);
com.a = +new Date();
com.$apply();
com.$nextTick().then(function () {
setTimeout(function () {
assert.strictEqual(com.$$nextTick, null, '$$nextTick should be cleared');
done();
}, 100);
});
com.$apply();
});


it('computed test', () => {
com.num = 11;
Expand Down
7 changes: 6 additions & 1 deletion packages/wepy/test/wxfake.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ module.exports = {
getWxPage: function () {
return {
$root: { '$wxapp': { app: 'app' } },
setData: function (v) { return v; },
setData: function (v, fn) {
if (typeof fn === 'function') {
fn();
}
return v;
},
$coma$comaa$tap: function () {
return arguments;
}
Expand Down

0 comments on commit 216c45b

Please sign in to comment.