knockout.punches icon indicating copy to clipboard operation
knockout.punches copied to clipboard

Feature Request: foreach: items as 'item'

Open aminroosta opened this issue 10 years ago • 8 comments

instead of "foreach: { data: items, as: 'item' }" binding , plz provide "foreach: items as 'item' " syntax .

thanks .

aminroosta avatar Sep 10 '14 22:09 aminroosta

Thanks for the suggestion. Is there a precedent for this syntax?

mbest avatar Sep 10 '14 22:09 mbest

i am just thinking a syntax like this "foreach: items as 'item' " will be cleaner . maybe "foreach: items as 'item' | filter1:arg1 | filter2 " will be even better .

like angularjs ng-repeat directive .

aminroosta avatar Sep 11 '14 07:09 aminroosta

+1 for this feature. It is much easier to remember.

damiandennis avatar Dec 22 '14 19:12 damiandennis

Ember's #each HandleBars helper supports something similar:

{{#each person in people}}
...
{{/each}}

aldendaniels avatar Jan 26 '15 05:01 aldendaniels

I found a use for this feature in ko. Where "foreach: { data: items, as: 'item' }" is used to go through array of strings rather than objects.

Please add this feature.

CarlosOnline avatar Apr 11 '15 15:04 CarlosOnline

I can second that this is one thing I miss from angular, {{foreach: item in items}} or {{foreach: items as 'item'}} would work for me, both are very readable syntax in my opinion. That being said, I don't mind just using ko's syntax either, so this really isn't a huge deal to me.

jlaustill avatar Dec 30 '15 21:12 jlaustill

Personally I have switched to aurelia - it has more modern features such as templates, latest java syntax, and a very intuitive binding syntax. Its still beta, but also very functional.

CarlosOnline avatar Dec 31 '15 16:12 CarlosOnline

@jlaustill I tried once a custom node preprocessor to have a very simple angular style of foreach. knockout.punches has a method to add one - addNodePreprocessor. It worked pretty cool and I also created custom attributes like ko-if and ko-unless. (e.g. <div ko-unless="condition"></div>). The implementation is very simple as it's just some markup wrapping and basic parsing for foreach.

<div ko-foreach="items as $item">
  <!-- The DIV element is repeated along with the inside content -->
</div>

crissdev avatar Dec 31 '15 17:12 crissdev