Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger itemView.onShow in appendBuffer if collectionView is already shown #866

Merged
merged 1 commit into from
Jan 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions spec/javascripts/collectionView.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,10 @@ describe("collection view", function(){
var isBuffering, iv, cv, collection, cvInstance;

iv = Marionette.ItemView.extend({
template: _.template("<div>hi mom</div>"),
onShow: function() {
isBuffering = cvInstance.isBuffering;
}
template: _.template("<div>hi mom</div>"),
onShow: function() {
isBuffering = cvInstance.isBuffering;
}
});

cv = Marionette.CollectionView.extend({
Expand All @@ -833,9 +833,29 @@ describe("collection view", function(){
});

it("collectionView should not be buffering after reset on itemView show", function() {
isBuffering = void 0;
collection.reset([{}]);
expect(isBuffering).toBe(false);
});

describe("item view show events", function () {
var showCalled;
beforeEach(function () {
showCalled = false;
iv.prototype.onShow = function () { showCalled = true; };
});

it("collectionView should trigger the show events when the buffer is inserted and the view has been shown", function () {
collection.reset([{}]);
expect(showCalled).toEqual(true);
});

it("collectionView should not trigger the show events if the view has not been shown", function () {
cvInstance = new cv({collection: collection});
cvInstance.render();
expect(showCalled).toEqual(false);
});
});
});

describe("when a collection view is not rendered", function() {
Expand Down
2 changes: 1 addition & 1 deletion spec/javascripts/compositeView.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ describe("composite view", function(){
});
});

describe("when workign with a composite and recursive model", function(){
describe("when working with a composite and recursive model", function(){
var treeView;

beforeEach(function(){
Expand Down
16 changes: 14 additions & 2 deletions src/marionette.collectionview.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Marionette.CollectionView = Marionette.View.extend({
// it's much more performant to insert elements into a document
// fragment and then insert that document fragment into the page
initRenderBuffer: function() {
this.elBuffer = document.createDocumentFragment();
this.elBuffer = document.createDocumentFragment();
this._bufferedChildren = [];
},

startBuffering: function() {
Expand All @@ -31,9 +32,19 @@ Marionette.CollectionView = Marionette.View.extend({
},

endBuffering: function() {
this.isBuffering = false;
this.appendBuffer(this, this.elBuffer);
this._triggerShowBufferedChildren();
this.initRenderBuffer();
this.isBuffering = false;
},

_triggerShowBufferedChildren: function () {
if (this._isShown) {
_.each(this._bufferedChildren, function (child) {
Marionette.triggerMethod.call(child, "show");
});
this._bufferedChildren = [];
}
},

// Configured the initial events that the collection view
Expand Down Expand Up @@ -287,6 +298,7 @@ Marionette.CollectionView = Marionette.View.extend({
// in order to reduce the number of inserts into the
// document, which are expensive.
collectionView.elBuffer.appendChild(itemView.el);
collectionView._bufferedChildren.push(itemView);
}
else {
// If we've already rendered the main collection, just
Expand Down
1 change: 1 addition & 0 deletions src/marionette.compositeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Marionette.CompositeView = Marionette.CollectionView.extend({
appendHtml: function(compositeView, itemView, index){
if (compositeView.isBuffering) {
compositeView.elBuffer.appendChild(itemView.el);
compositeView._bufferedChildren.push(itemView);
}
else {
// If we've already rendered the main collection, just
Expand Down