I recently upgraded an existing project to KnockoutJS 2.0 from KnockoutJS 1.2.1 (although I started using the previous version). Starting with the update, I noticed that afterRender seems to fire before the elements are actually completely inside the html.
I looked around a bit, and it looks like this is the intended behavior on several other issues in this area:
Why are div templates displayed as ": hidden" in afterRender?
The problem manifests itself in Jquery Validate, where I apply some rules to the elements and this tells me that the elements do not exist. Bizarrely, this worked well in 1.2.1. I'm not sure if this is due to the fact that afterRender was handled differently in previous versions or if changes in the general system of native templates lead to behavior differently ...
If changes have occurred or is this the intended behavior, is there a way to find out when the elements of the template really entered the html, where should they be? I know that the loaded elements are passed back through the afterRender callback arguments, but is it safe to use them for anything contextual to manipulate Dom at this point?
Edit
I put together an example of my specific problem: An example showing the problem
If you look at the afterRender logic for each template, it just does some simple validation, but whenever you try to use elements that it just explodes, however, if you choose the verification logic, it works fine.
I am more than happy to raise my hands if I did something wrong and try to fix it, but I honestly donβt know what the problem is, because everything works in isolation ...
source share