I'm trying to use the view helper inside the {{#each}} template blocks without using global paths (my controllers create and destroy their own views).
Examples. Given the view using the myList array property and the itemButton child view:
It will work
<script type="text/x-handlebars" name="my-list-view"> {{#each myList}} {{view App.myListView.itemButton}} {{title}} {{/each}} </script>
It will not be:
<script type="text/x-handlebars" name="my-list-view"> {{itemButton}} {{#each myList}} {{view itemButton}} {{title}} {{/each}} </script>
It seems I canβt access the parent view from each view helper (or actually access anything other than the properties of the repeating objects).
Hacker workarounds I came up with:
- Add the view that I want to use for the elements I'm repeating.
or
- Creating a View Collection in App.myListView
- Create an itemViewClass in this collection view class
- Move itemButton view inside itemViewClass
- Replace {{#each}} with {{#collection}}
or
- Create your own descriptor helper for iteration.
Both of these parameters seem terrible. Of course, there is a better alternative than creating 2 new classes (and nesting 4 types in depth) to iterate over the list. Is there a helper helper that I can use instead?
Workarounds
Option # 1: changing the content array
http://jsfiddle.net/FQEZq/3/ Disadvantages: the need to add a view to an instance of each model for iteration only.
Option # 2: View a Custom Collection
http://jsfiddle.net/ST24Y/1/ Disadvantages: now you have two additional views that you don't need / need, and less control over the markup. Links from the child view to the parent instance now require parentView.parentView.parentView.
Billiam
source share