I’m planning to convert a Knockout based page to native templating. However, I have a couple of questions about the limitations of native templating compared to jQuery Templates.
With Knockout native templating can I reference a function within an ‘if’ control?
jQuery Templates:
{{if GetFirstWord(ProductName) == "Premier"}}
Native Templates (is this possible)
<!-- ko if: GetFirstWord(ProductName) == "Premier" -->
Also, can I access an indexer like this using native templating:
{{each(i, d) Benefits}}
<div class="{{if i%2==0}}even{{else}}odd{{/if}}">
{{/each}}
First one: Yes, it’s possible, and most likely you won’t need to change much code. However, you might want to do anyway in order to suit a better architecture in KnockoutJS. It’s not only for KO, but also a better design pattern that’s more maintainable.
For instance, this
has the same functionality than
and in your code you have something like
(This was a quick example, you can design your code in a more scalable way through dynamic templating)
Second one: Yes, but only from version 2.1 through the
$indexkeyword. You can read more about the discussion here and some documentation here.As with the first point, you might want to change some of your javascript code to not give the template that much logic.
in native KnockoutJS templating
a better approach could be
and you do the logic in your js, maybe just once for your array.