gpt4 book ai didi

jquery - Ractive 组件的生命周期

转载 作者:行者123 更新时间:2023-12-01 02:42:45 24 4
gpt4 key购买 nike

除了调用 init 之外,我不确定 Ractive 组件的生命周期是什么。例如,什么时候直接访问 DOM 是安全的?我尝试使用流行的迷你图 jQuery 插件,但我从未让它工作。我使用了一个指令 - 瞧 - 可爱的迷你图。

有关于组件在 Ractive 中接收哪种类型的事件和回调的文档吗?

最佳答案

简短的回答是,在最新的稳定版本中,它是未定义的 - 在某些情况下,组件的 init 方法在组件附加到 DOM 之前被调用。这是一个错误。

在下一个版本0.4.0中不再是这种情况:http://cdn.ractivejs.org/edge/Ractive.min.js

从 0.4.0 开始,它看起来像这样:

Ractive.components.sparkline = Ractive.extend({
beforeInit: function (options) {
// called before any setup happens, in case you
// need to transform `options` in any way
},
init: function (options) {
// called immediately after the initial render,
// when the component is in the DOM
},
complete: function () {
// called when any initial transitions have
// completed
}
});

在组件从 DOM 中删除之前,将触发 teardown 事件 - 例如,在您的 init 方法中,您可以添加一些代码来执行任何必要的清理(如果有):

init: function () {
this.on('teardown', function () {
// cleanup
}
},
...

我欢迎您对您认为缺失的其他事件/钩子(Hook)提供任何反馈(如果有的话)。

关于jquery - Ractive 组件的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21843341/

24 4 0