Skip to content

Commit

Permalink
allow override of rendering after a collection sort
Browse files Browse the repository at this point in the history
  • Loading branch information
ahumphreys87 committed Jul 14, 2014
1 parent 719b300 commit 6d8b7b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/marionette.collectionview.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ will provide features such as `onShow` callbacks, etc. Please see
* [CollectionView: Automatic Rendering](#collectionview-automatic-rendering)
* [CollectionView: Re-render Collection](#collectionview-re-render-collection)
* [CollectionView's attachHtml](#collectionviews-attachhtml)
* [CollectionView's resortView](#collectionviews-resortview)
* [CollectionView's children](#collectionviews-children)
* [CollectionView destroy](#collectionview-destroy)

Expand Down Expand Up @@ -707,6 +708,25 @@ Overrides of `attachHtml` that don't take into account the element
buffer will work fine, but won't take advantage of the 60x performance
increase the buffer provides.
## CollectionView's resortView
By default the `CollectionView` will maintain the order of its `collection`
in the DOM, however on occasions the view may need to re-render to make this
possible, for example if you were to change the comparator on the collection.
By default `CollectionView` will call `render` when this happens but there are
cases where this may not be suitable, for instance when sorting the `children`
in a `CompositeView`, in this instance you want to only render the internal
collection.
```js
var cv = new Marionette.CollectionView({
collection: someCollection,
resortView: function() {
// provide custom logic for rendering after sorting the collection
}
});
```
## CollectionView's children
The CollectionView uses [Backbone.BabySitter](https://github.com/marionettejs/backbone.babysitter)
Expand Down
12 changes: 10 additions & 2 deletions src/marionette.collectionview.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,25 @@ Marionette.CollectionView = Marionette.View.extend({
return this;
},

// Render view after sorting. Override this method to
// change how the view renders after a `sort` on the collection.
// An example of this would be to only `renderChildren` in a `CompositeView`
// rather than the full view.
resortView: function() {
this.render();
},

// Internal method. This checks for any changes in the order of the collection.
// If the index of any view doesn't match, it will render.
_sortViews: function(){
_sortViews: function() {
// check for any changes in sort order of views
var orderChanged = this.collection.find(function(item, index){
var view = this.children.findByModel(item);
return view && view._index !== index;
}, this);

if (orderChanged) {
this.render();
this.resortView();
}
},

Expand Down

0 comments on commit 6d8b7b8

Please sign in to comment.