In Marionette, the standard way to add a new item to a collection in views is to use the jQuery append
method. The CollectionView
type has an appendHtml
method that is used to actually add. (see http://derickbailey.github.com/backbone.marionette/docs/backbone.marionette.html#section-24 )
You can easily override this method in your specific collection view, and attach a new model wherever it is.
In your case, if you always want to add a new model to the top of the list, itβs very simple to change the look of the collection to do this:
Backbone.Marionette.CollectionView.extend({ appendHtml: function(cv, iv){ cv.$el.prepend(iv.el); } });
Notice that cv
is an instance of the collection view, and iv
is an instance of the element view for the model in the collection.
If you need to do more complex things, for example, find the exact position in an existing collection of HTML nodes, you can do this in the appendHtml
function appendHtml
. Of course, this becomes more difficult than just preend instead of append, but it is still possible.
Hope this helps.
source share