Ryan, many thanks for your very detailed response. I am very new to KO
and am trying to keep things (code/my life) as simple as possible.
Your example does of course work, however it adds quite a bit of
complexity to something that is so simple to do with jQuery tmpl
{{each}}.
Further I don't see how this addresses my second issue "of nested
{{each}}'s and nested array content in the viewmodel.", which I assume
you missed. To clarify this, it isn't just about striping (and nested
striping), but also accessing the content of nested arrays in the
template.
-Neville
On Apr 13, 12:38 pm, rpn <
rnieme...@gmail.com> wrote:
> This is an interesting question. I would love to hear how others have solved
> this in a clever way.
>
> I think that the best solution needs to work properly even when items are
> added or removed from the array. Also, I think that the template for each
> item should not need to create a dependency on the entire array, otherwise
> it defeats the benefits of using foreach.
>
> Some options that I was considering:
>
> - trying to use the "afterAdd", "beforeRemove", or particularly the
> "afterRender" callbacks on the template binding. These always work on the
> DOM elements associated with the individual items in the array though, so
> that doesn't seem to give us any better access than what we have in the
> template itself.
> - calling a function that just alternates the value that it returns to
> alternate the style that is used. This would work initially, but would not
> work when we remove an item in the middle. Additionally, if we keep reusing
> this function on multiple tables, then we can't guarantee that the first row
> will be consistently light or dark (minor point)
> - building a special observableArray that maintains the index (as an
> observable) along with the value and updates the index whenever an operation
> is performed on the array. Seems like it would take to much code to make
> this happen for too little benefit.
> - using ":nth-child(odd)" or ":nth-child(even)" CSS selectors. This