Yao 1d7c23b289
[refactor] Toast: 升级为自定义组件 (#191)
* toast 组件初始化

* toast 增加示例 && 逻辑补全

* toast 支持直接 loading 调用

* 增加 toast readme

* 去除多余的引用
2018-04-12 21:58:54 +08:00

61 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let timeoutData = {
timeoutId: 0,
toastCtx: null
}
function Toast(options = {}, pageCtx) {
let ctx = pageCtx;
if (!ctx) {
const pages = getCurrentPages();
ctx = pages[pages.length - 1];
}
const toastCtx = ctx.selectComponent(options.selector);
if (!toastCtx) {
console.error('无法找到对应的toast组件请于页面中注册并在 wxml 中声明 toast 自定义组件');
return;
}
if (timeoutData.timeoutId) {
Toast.clear();
}
toastCtx.show({
...options,
show: true
});
const timeoutId = setTimeout(() => {
toastCtx.clear();
}, options.timeout || 3000);
timeoutData = {
timeoutId,
toastCtx
}
};
// 清理所有 toast
Toast.clear = function() {
clearTimeout(timeoutData.timeoutId);
try {
timeoutData.toastCtx && timeoutData.toastCtx.clear();
} catch (e) {}
timeoutData = {
timeoutId: 0,
toastCtx: null
}
}
// 显示 loading
Toast.loading = function(options = {}) {
Toast({
...options,
type: 'loading'
});
}
module.exports = Toast;