-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/foreach aliases #79
Conversation
very nice. How do I do something different for the first or last item in a loop. For example I may want to generate:
or
|
With the current limitation that you can only bind to variables in the run api, you can do wither <tr>
<th>sales</th>
<td data-f-repeat="sales"></td>
</tr> or (underscore syntax) <ul data-f-foreach="somearray">
<li> <%= (index === 0) ? "first" : value %> </li>
</ul> The 'last' part is interesting. My first instinct was to suggest creating a custom converter (about 10 lines of JS) but this is probably too common a use-case. I could expose a 'length' property, but this gets tricky with nested loops & can't think of a good syntax to let you alias length..Ideas? |
if you do first that's good enough. you can make a comma separated list with each element being ,X and having the first one have no comma before it. |
The easier solution than that is:
A more generic/ better solution may be to expose the array itself as a variable.
You'd need to know enough js to do '-1' to compare of course. I'll create a separate ticket for this. |
2f5cc60
to
6e74e5a
Compare
see #82, from this branch, for additional test cases (and question on a failing case, although that may be user error). |
tested and verified
|
Introduces new syntax for foreach indices, to make them more usable in general, helping with nesting them in particular. You can now do:
(assuming fruits is an array in the model)
So while nesting you can now do:
Of course if you don't use aliases and do
<ul data-f-foreach="sales">
you still have access to thekey/index
andvalue
variables (there are not available if you alias them to something else).There is a tangential breaking change, though I don't think it'll break anything built so far-- in previous versions, bodies of
foreach
always had access to bothkey
andindex
to return the same thing. Now you getkey
if you're looping through an object andindex
if you're looping through an array but never both.Also part of this PR is updating lodash to 4.11.1. I mistakenly thought I needed that for this feature, but turns out I didn't. I may break apart lodash into a separate PR to be merged whenever we update Contour.
cc @mmrj
Closes #30