OWL教程8 辅助工具类
1.EventBus
一个简单的事件总线
它是一个简单的EventBus,具有与通常的DOM元素相同的API,以及一个额外的触发器方法来调度事件:
const bus = new EventBus();
bus.addEventListener("event", () => console.log("something happened"));
bus.trigger("event"); // 'something happened' is logged
fatux: 怎么传参呢?
2 loadfile
从服务器加载文件的助手
loadFile是一个获取文件的辅助函数。它只是执行一个GET请求,并在promise中返回结果字符串。这个函数的初始用例是加载一个模板文件。例如:
const { loadFile } = owl;
async function makeEnv() {
const templates = await loadFile("templates.xml");
// do something
}
3 markup
用于定义表示HTML的字符串的工具函数(不应转义)
通常跟t-out 指令一起用,返回原始的html标签
4 status
获取组件状态的工具函数(new,mounted 或者destroyed)
const { status } = owl;
// assume component is an instance of a Component
console.log(status(component));
// logs either:
// - 'new', if the component is new and has not been mounted yet
// - 'mounted', if the component is currently mounted
// - 'destroyed' if the component is currently destroyed
5 validate
验证对象是否满足指定的模式
validate函数是一个验证给定对象是否满足指定模式的函数。它实际上被Owl自己用来执行props验证 props validation。例如:
validate(
{ a: "hey" },
{
id: Number,
url: [Boolean, { type: Array, element: Number }],
}
);
// throws an error with the following information:
// - unknown key 'a',
// - 'id' is missing (should be a number),
// - 'url' is missing (should be a boolean or list of numbers),
6 whenReady
实用程序函数,用于在DOM准备好时执行代码
whenReady函数返回一个在DOM准备好时解析的Promise(如果还没有准备好,则直接解析)。如果以回调作为参数调用,则在DOM准备好(或直接)后立即执行回调。
const { whenReady } = owl;
await whenReady();
// do something
or alternatively:
whenReady(function () {
// do something
});