marionettejs/backbone.marionette — pull request #353 by gurdotan (2012-11-25 13:15:36+00:00)
Title: Append an itemView to a the correct index in a collectionView

When working with a collection view or composite view that are bound to a Backbone collection, adding new models to the collection will always append them to the end of the view regardless of whether `{at : index}` was passed. So if I have a collection of 2 models and I add a new model in between them, the view will render the model last instead of 2nd:
``
collection.toJSON() // => [{name : "Luke"}, {name : "Anakin"}]
collectionView.el.innerHTML
// =>
Luke

Anakin

collection.add({name : "Yoda"}, {at : 1});
collectionView.el.innerHTML
// =>
Luke

Anakin

Yoda

``

The solution proposed is to modify the `appendHtml` methods so that they do following:
1. If the index is 0, add itemView's element with `$.prepend`. This ensures that when rendering the collection for the first time, or when doing `collection.add(model, {at : 0})`, the element is added as the collection view's first child.
2. Otherwise, select the nth-child of the collection view's children and append the itemView's element with `$.after`. This will guarantee that new collections are rendered correctly and that any new models inserted to an existing collection with an arbitrary index other than 0 are rendered in the correct position within the collection view's `el`.