Containerless template bindings?

1,070 views
Skip to first unread message

Jeremy Rosenberg

unread,
Oct 27, 2011, 5:18:13 PM10/27/11
to KnockoutJS
Basically, I have a table, and in the tbody, I have rows rendered from
an observableArray, plus an extra row not in that array but sometimes
using the same template.

To support this, I ended up using something along these lines:
<tbody>
<!-- ko template: { name: 'RowTemplate', foreach: ExistingItems } --
><!-- /ko -->
<!-- ko if: NewItem -->
<!-- ko template: { name: 'RowTemplate', data: NewItem } --><!-- /
ko -->
<!-- /ko -->
<!-- ko ifnot: NewItem -->
<tr><td colspan="x">Click here to add a new item</td></tr>
<!-- /ko -->
</tbody>

Is this kosher?

I can't use a data-bind attribute on tbody, because I need the
conditional logic for the "new" row after, still within tbody. There
is no element "in between" the tbody and tr's that I could stick a
data-bind attribute on. So it seems necessary to use containerless
bindings. However, all of the given containerless bindings use
anonymous templates except for the template binding. So that's what
I'm using.

I think it feels so hackish to me only because of the necessary
closing /ko comments after each template binding comment. There's no
body to them, and indeed there couldn't be, so it almost seems like
the template binding should be a self closing tag comment, but such a
thing doesn't exist right now.

Any thoughts?

rpn

unread,
Oct 27, 2011, 5:57:27 PM10/27/11
to knock...@googlegroups.com
Tables can have multiple tbody tags.  If you are feeling uncomfortable with your current code, then you could probably just use multiple tbodys.

Jeremy Rosenberg

unread,
Oct 28, 2011, 11:39:23 AM10/28/11
to KnockoutJS
I did not know that. Learn something new every day. Thanks.

I guess the question is, then, is the template comment binding safe to
use, as in likely to stick around for 1.3 final and maintained for
backwards compatibility? It wasn't explicitly documented, and it
would just be nice to know for future reference.

rpn

unread,
Oct 28, 2011, 11:57:14 AM10/28/11
to knock...@googlegroups.com
Yes, "template" is intentionally supported by the containerless bindings.  
Reply all
Reply to author
Forward
0 new messages