foreach: $array, $isFirst, $isLast, $isEven, $isOdd, $next, $previous - #2415
foreach: $array, $isFirst, $isLast, $isEven, $isOdd, $next, $previous#2415barsh wants to merge 1 commit into
Conversation
|
Conceptually I think this is useful, but we'd need to make sure it's consistent with the fast-foreach API in tko (since folks may move back-and-forth); the tko API is more complex since it's |
|
I wonder why you want to perform such code in the template rather than adding a model.filteredArray = ko.pureComputed(() => model.array.filter(el => el > 1))I don't see a huge downside of adding an |
|
@bikeshedder: Thanks for jumping in with a great point that I agree with. However, the situation that inspired this pull request was a bit more complex than the simplified example provided here and while your suggestion could be applied to that situation too, filtering in the template itself in that particular case led to a more developer-friendly solution. |
|
I just realized that with the addition of the For example... <!-- ko let: { myFilteredArray: arr().filter(...) } -->
<ul data-bind="foreach: myFilteredArray">
<!-- myFilteredArray is still in scope -->
</ul>
<!-- /ko --> (I haven't actually tested this exact code) |
There are situations where you want access to the array bound to a foreach, for example:
This produces
instead of
This PR introduces
$arrayto the context, for example:Which produces the desired result of
In addition to
$arraythe following additional helpers were added:$isFirst, $isLast, $isEven, $isOdd, $next, $previousFor example:
produces: