whiteatom
whiteatom

Reputation: 1455

Why is knockout firing afterRender event twice?

I have a knockout template that is loading perfectly, but the afterRender event is firing twice.. I thought it was some problem in my data object causing the function to get called once accidentally and once by the afterRender, but I have reduced it to an alert, and it still get's called twice.

Here's the html code...

<body>
 <div id="control-wrap" data-bind="template: { name: 'control_bar', data: App.Controls, afterRender: function(){ alert('test');} }"></div>
 <div id="map_canvas" style="width:100%; height:100%"></div>
 <div id="info-window-container" data-bind="template: { name: 'info-yacht-racing', foreach: App.Transponders.transponders, afterRender: App.Tracker.infoWindowInit}"></div>
 <div id='notice-overlay'></div>
</body>

The biggest problem is that I have no idea where to troubleshoot this form? Any idea? can i use the Chrome inspector to track what is triggering the event? Would the google map renderer being mucking something up? would a foreach with the template cause the parent template call to trigger more then once?

whiteatom

UPDATE: The strangeness continues... If I make the template blank, it only fires once. But if there is any content (even just a '.'), the afterRender fires twice. No here's the strange part.. once is BEFORE the content appears on the page, the second is after....

Upvotes: 2

Views: 2351

Answers (1)

whiteatom
whiteatom

Reputation: 1455

Found it....

https://github.com/fazzamar/Knockout.js-External-Template-Engine/commit/5b053acd0465cd5c98b947d97cbd4d2e675c9f52

This appears to be a bug in the external template engine. Take out one self. reference and the problem is solved.

Thanks for the help RP, you definitely caused me to do some more simplification of the code - which let me solve it.

Cheers!

Upvotes: 2

Related Questions