Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run code after Knockout JS has updated the 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> 

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?

like image 204
Magpie Avatar asked Jan 18 '13 12:01

Magpie


People also ask

Do people still use KnockoutJS?

KnockoutJS is far from dead, and it's actually still being improved and evolved (see Technical Knockout) but newer frameworks seem a far better bet for our needs, considering activity and performance.

How do I use KnockoutJS in HTML?

It is very easy to use KnockoutJS. Simply refer the JavaScript file using <script> tag in HTML pages. A page as in the following image will be displayed. Click on download link and you will get the latest knockout.


1 Answers

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();     } } 
like image 184
KodeKreachor Avatar answered Oct 08 '22 07:10

KodeKreachor