Knockout JS 更新 DOM 后如何运行代码 [英] How to run code after Knockout JS has updated the DOM

查看:15
本文介绍了Knockout JS 更新 DOM 后如何运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我观点的一部分,我有:

As part of my view I have:

<ul data-bind="foreach: caseStudies">
    <li><a data-bind="text: title, attr: { href: caseStudyUrl }"></a></li>
</ul>

我想在淘汰赛更新 DOM 后运行一些第 3 方代码.

I want to run some 3rd Party code once knockout has updated the DOM.

caseStudies(data);
thirdPartyFuncToDoStuffToCaseStudyLinks(); <-- DOM not updated at this point.

知道如何在正确的时间进入淘汰赛并调用它吗?

Any idea on how I can hook into knockout to call this at the correct time?

推荐答案

使用 afterRender 绑定可以帮助您.

Using the afterRender binding can help you.

<ul data-bind="foreach: { data:caseStudies, afterRender:checkToRunThirdPartyFunction }">
    <li><a data-bind="text: title, attr: { href: caseStudyUrl }"></a></li>
</ul>


function checkToRunThirdPartyFunction(element, caseStudy) {
    if(caseStudies.indexOf(caseStudy) == caseStudies().length - 1){
        thirdPartyFuncToDoStuffToCaseStudyLinks();
    }
}

这篇关于Knockout JS 更新 DOM 后如何运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆