powmedia / backbone-forms

Form framework for BackboneJS with nested forms, editable lists and validation
MIT License
2.17k stars 413 forks source link

Pass id through to child list items, and iterate index for each item. #393

Open rjmackay opened 10 years ago

rjmackay commented 10 years ago

Making it easier to work with list items by giving them unique ids.

fonji commented 10 years ago

+1

iabw commented 10 years ago

I haven't explicitly tested it, but I think that this has problems with lists that are meant to be sorted or re-ordered, such as this plugin

If you have 4 items such as this:

[{id:1},{id:2},{id:3},{id:4}]

and you move the last item to the front of the array

[{id:4},{id:1},{id:2},{id:3}]

Your ids (wth the implied order because you're using the length attribute for id) are now confusing.

Even worse, if you now remove the last item

[{id:4},{id:1},{id:2}]

but then add a new one

[{id:4},{id:1},{id:2},{id:4}] //length is 4 again

We have duplicate IDs.

I do not think items.length should be used to generate the id. Instead, I would recommend using the view's cid attribute.

Is there a specific reason you needed the index in the id string? If so, is there a reason you can't index the item against list.items?