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 authored and samccone committed Jul 30, 2014
1 parent 3cffecd commit 9cb57d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
19 changes: 19 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 @@ -737,6 +738,24 @@ 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`, 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
13 changes: 13 additions & 0 deletions spec/javascripts/sortedViews.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ describe('collection/composite view sorting', function() {
collection: this.collection,
});

this.sinon.spy(this.collectionView, 'resortView');
this.sinon.spy(this.compositeView, 'resortView');

this.collectionView.render();
this.compositeView.render();
});
Expand Down Expand Up @@ -109,6 +112,11 @@ describe('collection/composite view sorting', function() {
expect(this.collectionView.$el).to.have.$text('12345');
expect(this.compositeView.$el).to.have.$text('12345');
});

it('should call resortViews', function() {
expect(this.collectionView.resortView).to.have.been.calledOnce;
expect(this.compositeView.resortView).to.have.been.calledOnce;
});
});
});

Expand Down Expand Up @@ -155,6 +163,11 @@ describe('collection/composite view sorting', function() {
expect(this.collectionView.$el).to.have.$text('321');
expect(this.compositeView.$el).to.have.$text('321');
});

it('should call resortViews', function() {
expect(this.collectionView.resortView).to.have.been.calledOnce;
expect(this.compositeView.resortView).to.have.been.calledOnce;
});
});

describe('and adding a new child', function() {
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 9cb57d4

Please sign in to comment.