The features needed are:
- Automatically add new blank input fields at the end of the list as needed
- Allow the removal of any items from the list at any point
- Allow changing any items in the list of inputs
Given lots of help from the Aurelia Gitter channel especially by Jeremy Danyow (@jdanyow) and Io Sulfur (@iosulfur) who created several Plunkrs zeroing in on the right solution, we came up with this approach as demonstrated in the Plunkr below.
Anytime you change the blank item field at the end, it will add another empty input field at the end. If you click on the 'X' button, it will remove that item.
The change.delegate on line 8 adds the blank line using the current index value of the repeat and the change event. You'll see how they are used in the app.js file below.
The click.delegate very simply removes the input field by splicing out that item from the items array.
The first thing to do is to add a blank item at the end of the items array before we even present it to the View in line 7 of app.js
The addBlank function first sets the current item (based on the $index value passed from the change.delegate function) to the event.target.value and then blanks out the event.target.value. We blank out the event.target.value as otherwise that value gets added to the end of the list of input fields.
We then check to see if the last input field is empty or not, and if it returns true, we push an empty string onto items. (Note: if anyone can explain why you get the event.target.value instead of the empty '' string unless we reset the event.target.value, please post it in the comments - because I've not been able to figure it out.
The removeItem is pretty self-explanatory - so that's all. Thanks for reading, hope it is useful.
Added (2015-08-30) : When I tried this with an object as the item, I didn't need lines 12 and 13 in app.js.