gpt4 book ai didi

javascript - 为什么模板绑定(bind)文档中提到了 beforeRemove 事件,而它没有执行任何操作?

转载 作者:行者123 更新时间:2023-12-02 17:28:30 24 4
gpt4 key购买 nike

我一直在阅读 knockoutjs docs about the template binding 。具体来说,我希望能够 Hook 渲染模板的时刻,以及渲染模板被删除之前的时刻(例如,通过更改绑定(bind)的 name 属性)。文档似乎可能表明 afterRender 和 beforeRemove 事件是我感兴趣的。

所以我有以下标记:

<script type="text/html" id="templ1">
foo!
</script>
<script type="text/html" id="templ2">
bar!
</script>
<!-- ko if: templateName()&&templateName().length>0 -->
<div data-bind="template:{name:templateName(),afterRender:ar,beforeRemove:br}">
</div>
<!-- /ko -->

以及以下 JavaScript

$(function(){
var vm={
templateName:ko.observable("templ1"),
ar:function(){
alert("afterRender");
},
br:function(){
alert("beforeRemove");
}
};
ko.applyBindings(vm);
setTimeout(function(){
vm.templateName("templ2");
},5000);
});

afterRender 事件正常触发。 beforeRemove 事件不会触发。

这里有什么?我是否误解了文档?

有一个fiddle here .

最佳答案

http://knockoutjs.com/documentation/template-binding.html#note-4-using-afterrender-afteradd-and-beforeremove 中所述,beforeRemove 事件是 foreach 绑定(bind)的一部分,并且在由于数组上的元素删除而删除 DOM 节点之前发生。

beforeRemove — is invoked when an array item has been removed, but before the corresponding DOM nodes have been removed. If you specify a beforeRemove callback, then it becomes your responsibility to remove the DOM nodes.

关于javascript - 为什么模板绑定(bind)文档中提到了 beforeRemove 事件,而它没有执行任何操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23297827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com