Dinamically added template is not interpreted

53 views
Skip to first unread message

Mario Valle

unread,
Dec 11, 2017, 3:53:12 AM12/11/17
to KnockoutJS
During the startup of my application, I instantiate a template with:
$someContainer.html(`<div data-bind="template: {name: 'my-template'}"></div>`);
and it works perfectly.

Now, during the application life, I destroy the content of $someContainer and later rerun the above code line to refill it.

But this time I see in place the <div data-bind="template: {name: 'my-template'}"></div> in the DOM, but Knockout has not been run on it to interpret its content.

The workaround I devised is to run ko.applyBindings(viewmodel, $sameContainer[0]); after recreation of the template.

I'm doing something wrong, or is there any better method?
Thanks for your help!
mario

Gunnar Liljas

unread,
Dec 11, 2017, 12:11:37 PM12/11/17
to knock...@googlegroups.com
1. Why are you adding the code dynamically?
2. Why are you removing it?

Mario Valle

unread,
Dec 12, 2017, 2:10:59 AM12/12/17
to KnockoutJS
The user can close the bottom panel to give more room to the upper one. Then, when he needs to access the bottom panel functionalities, pushes a toolbar button and the panel reappears.
Tried other methods to hide the bottom panel, but this is the only one that works.

Jean-Sebastien Binette

unread,
Dec 12, 2017, 8:40:58 AM12/12/17
to KnockoutJS
there might not be enough details here to make a full assessment but I’d set the template to an observable and then programmatically set it to an onject like you have and another one with an ‘empty’ div say. So it will appear and dissapeear as you see fit.

Mario Valle

unread,
Dec 12, 2017, 1:27:43 PM12/12/17
to KnockoutJS
Nice idea, but the template simply fills a frame of a GoldenLayout layout, so putting an empty <div> simply leaves the layout frame in place, but empty.
Anyway, my workaround seems working perfectly.
Thanks!
mario

Gunnar Liljas

unread,
Dec 12, 2017, 4:23:49 PM12/12/17
to knock...@googlegroups.com
A toggling observable and hiding the "template" in an <--ko if:theobservable--> block should do the trick.

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages