Skip to content

Instantly share code, notes, and snippets.

@jasonLaster
Created April 14, 2015 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonLaster/52a36178a996ad03e6a5 to your computer and use it in GitHub Desktop.
Save jasonLaster/52a36178a996ad03e6a5 to your computer and use it in GitHub Desktop.
commit 5df7838b2cbcce5764fa182cd4a10ef4b145a2e6
Author: Jason Laster <jason.laster.11@gmail.com>
Date: Tue Apr 14 19:15:06 2015 -0400
Remove empty views
diff --git a/src/collection-view.js b/src/collection-view.js
index 74c6ce5..b0eb21c 100644
--- a/src/collection-view.js
+++ b/src/collection-view.js
@@ -91,7 +91,6 @@ Marionette.CollectionView = Marionette.AbstractView.extend({
}
if (this._shouldAddChild(child, index)) {
- this.destroyEmptyView();
var ChildView = this.getChildView(child);
this.addChild(child, ChildView, index);
}
@@ -101,7 +100,6 @@ Marionette.CollectionView = Marionette.AbstractView.extend({
_onCollectionRemove: function(model) {
var view = this.children.findByModel(model);
this.removeChildView(view);
- this.checkEmpty();
},
_onBeforeShowCalled: function() {
@@ -186,9 +184,6 @@ Marionette.CollectionView = Marionette.AbstractView.extend({
}
},
- // Internal reference to what index a `emptyView` is.
- _emptyViewIndex: -1,
-
// Internal method. Separated so that CompositeView can append to the childViewContainer
// if necessary
_appendReorderedChildren: function(children) {
@@ -199,19 +194,14 @@ Marionette.CollectionView = Marionette.AbstractView.extend({
// more control over events being triggered, around the rendering
// process
_renderChildren: function() {
- this.destroyEmptyView();
- this.destroyChildren({checkEmpty: false});
+ this.destroyChildren();
var models = this._filteredSortedModels();
- if (this.isEmpty(this.collection, {processedModels: models})) {
- this.showEmptyView();
- } else {
- this.triggerMethod('before:render:collection', this);
- this.startBuffering();
- this.showCollection(models);
- this.endBuffering();
- this.triggerMethod('render:collection', this);
- }
+ this.triggerMethod('before:render:collection', this);
+ this.startBuffering();
+ this.showCollection(models);
+ this.endBuffering();
+ this.triggerMethod('render:collection', this);
},
// Internal method to loop through collection and show each child view.
@@ -256,83 +246,6 @@ Marionette.CollectionView = Marionette.AbstractView.extend({
}
return models;
},
-
- // Internal method to show an empty view in place of
- // a collection of child views, when the collection is empty
- showEmptyView: function() {
- var EmptyView = this.getEmptyView();
-
- if (EmptyView && !this._showingEmptyView) {
- this.triggerMethod('before:render:empty');
-
- this._showingEmptyView = true;
- var model = new Backbone.Model();
- this.addEmptyView(model, EmptyView);
-
- this.triggerMethod('render:empty');
- }
- },
-
- // Internal method to destroy an existing emptyView instance
- // if one exists. Called when a collection view has been
- // rendered empty, and then a child is added to the collection.
- destroyEmptyView: function() {
- if (this._showingEmptyView) {
- this.triggerMethod('before:remove:empty');
-
- this.destroyChildren();
- delete this._showingEmptyView;
-
- this.triggerMethod('remove:empty');
- }
- },
-
- // Retrieve the empty view class
- getEmptyView: function() {
- return this.getOption('emptyView');
- },
-
- // Render and show the emptyView. Similar to addChild method
- // but "add:child" events are not fired, and the event from
- // emptyView are not forwarded
- addEmptyView: function(child, EmptyView) {
-
- // get the emptyViewOptions, falling back to childViewOptions
- var emptyViewOptions = this.getOption('emptyViewOptions') ||
- this.getOption('childViewOptions');
-
- if (_.isFunction(emptyViewOptions)) {
- emptyViewOptions = emptyViewOptions.call(this, child, this._emptyViewIndex);
- }
-
- // build the empty view
- var view = this.buildChildView(child, EmptyView, emptyViewOptions);
-
- view._parent = this;
-
- // Proxy emptyView events
- this.proxyChildEvents(view);
-
- // trigger the 'before:show' event on `view` if the collection view
- // has already been shown
- if (this._isShown) {
- Marionette.triggerMethodOn(view, 'before:show', view);
- }
-
- // Store the `emptyView` like a `childView` so we can properly
- // remove and/or close it later
- this.children.add(view);
-
- // Render it and show it
- this.renderChildView(view, this._emptyViewIndex);
-
- // call the 'show' method if the collection view
- // has already been shown
- if (this._isShown) {
- Marionette.triggerMethodOn(view, 'show', view);
- }
- },
-
// Retrieve the `childView` class, either from `this.options.childView`
// or from the `childView` in the object definition. The "options"
// takes precedence.
@@ -459,25 +372,6 @@ Marionette.CollectionView = Marionette.AbstractView.extend({
return view;
},
- // check if the collection is empty
- // or optionally whether an array of pre-processed models is empty
- isEmpty: function(collection, options) {
- var models;
- if (_.result(options, 'processedModels')) {
- models = options.processedModels;
- } else {
- models = this.collection ? this.collection.models : [];
- models = this._filterModels(models);
- }
- return models.length === 0;
- },
-
- // If empty, show the empty view
- checkEmpty: function() {
- if (this.isEmpty(this.collection)) {
- this.showEmptyView();
- }
- },
// You might need to override this if you've overridden attachHtml
attachBuffer: function(collectionView) {
@@ -548,7 +442,7 @@ Marionette.CollectionView = Marionette.AbstractView.extend({
if (this.isDestroyed) { return this; }
this.triggerMethod('before:destroy:collection');
- this.destroyChildren({checkEmpty: false});
+ this.destroyChildren();
this.triggerMethod('destroy:collection');
return Marionette.AbstractView.prototype.destroy.apply(this, arguments);
@@ -558,18 +452,11 @@ Marionette.CollectionView = Marionette.AbstractView.extend({
// is holding on to, if any
destroyChildren: function(options) {
var destroyOptions = options || {};
- var shouldCheckEmpty = true;
var childViews = this.children.map(_.identity);
- if (!_.isUndefined(destroyOptions.checkEmpty)) {
- shouldCheckEmpty = destroyOptions.checkEmpty;
- }
this.children.each(this.removeChildView, this);
- if (shouldCheckEmpty) {
- this.checkEmpty();
- }
return childViews;
},
diff --git a/test/unit/collection-view.empty-view.spec.js b/test/unit/collection-view.empty-view.spec.js
index 64cc7e9..2036ba7 100644
--- a/test/unit/collection-view.empty-view.spec.js
+++ b/test/unit/collection-view.empty-view.spec.js
@@ -1,4 +1,4 @@
-describe('collectionview - emptyView', function() {
+xdescribe('collectionview - emptyView', function() {
'use strict';
beforeEach(function() {
diff --git a/test/unit/collection-view.filter.spec.js b/test/unit/collection-view.filter.spec.js
index 384ecc5..4ec038d 100644
--- a/test/unit/collection-view.filter.spec.js
+++ b/test/unit/collection-view.filter.spec.js
@@ -8,11 +8,11 @@ describe('collection view - filter', function() {
this.failModel = new Backbone.Model({foo: false});
this.collection = new Backbone.Collection();
- this.EmptyView = Backbone.Marionette.ItemView.extend({
- template: function() {
- return 'empty';
- }
- });
+ // this.EmptyView = Backbone.Marionette.ItemView.extend({
+ // template: function() {
+ // return 'empty';
+ // }
+ // });
this.ChildView = Backbone.Marionette.ItemView.extend({
template: function(data) {
@@ -29,7 +29,7 @@ describe('collection view - filter', function() {
});
this.CollectionView = Backbone.Marionette.CollectionView.extend({
- emptyView: this.EmptyView,
+ // emptyView: this.EmptyView,
childView: this.ChildView,
filter: this.filter,
collection: this.collection,
@@ -124,7 +124,7 @@ describe('collection view - filter', function() {
.and.calledWith(this.passView);
});
- it('should show the EmptyView', function() {
+ xit('should show the EmptyView', function() {
expect(this.collectionView.$el).to.contain.$text('empty');
});
});
@@ -165,7 +165,7 @@ describe('collection view - filter', function() {
beforeEach(function() {
this.filter.reset();
this.newFailModel = this.failModel.clone();
- this.sinon.spy(this.collectionView, 'showEmptyView');
+ // this.sinon.spy(this.collectionView, 'showEmptyView');
this.collectionView.onBeforeRenderCollection.reset();
this.collectionView.onRenderCollection.reset();
this.collection.reset([this.newFailModel]);
@@ -175,21 +175,21 @@ describe('collection view - filter', function() {
expect(this.collectionView.children.findByModel(this.newFailModel)).not.to.exist;
});
- it('should show the empty view', function() {
+ xit('should show the empty view', function() {
expect(this.collectionView.showEmptyView).to.have.been.calledOnce
.and.calledOn(this.collectionView);
});
- it('should contain the empty view in the DOM', function() {
+ xit('should contain the empty view in the DOM', function() {
expect(this.collectionView.$el).to.contain.$text('empty');
});
it('should not call onBeforeRenderCollection', function() {
- expect(this.collectionView.onBeforeRenderCollection).not.to.have.been.called;
+ expect(this.collectionView.onBeforeRenderCollection).to.have.been.called;
});
- it('should not call onRenderCollection', function() {
- expect(this.collectionView.onBeforeRenderCollection).not.to.have.been.called;
+ it('should call onRenderCollection', function() {
+ expect(this.collectionView.onBeforeRenderCollection).to.have.been.called;
});
});
@@ -222,25 +222,25 @@ describe('collection view - filter', function() {
beforeEach(function() {
this.collection.add(this.failModel);
this.collectionView = new this.CollectionView();
- this.sinon.spy(this.collectionView, 'showEmptyView');
+ // this.sinon.spy(this.collectionView, 'showEmptyView');
this.collectionView.render();
});
- it('should show the empty view', function() {
+ xit('should show the empty view', function() {
expect(this.collectionView.showEmptyView).to.have.been.calledOnce
.and.calledOn(this.collectionView);
});
- it('should contain the empty view in the DOM', function() {
+ xit('should contain the empty view in the DOM', function() {
expect(this.collectionView.$el).to.contain.$text('empty');
});
- it('should not call onBeforeRenderCollection', function() {
- expect(this.collectionView.onBeforeRenderCollection).not.to.have.been.called;
+ it('should call onBeforeRenderCollection', function() {
+ expect(this.collectionView.onBeforeRenderCollection).to.have.been.called;
});
- it('should not call onRenderCollection', function() {
- expect(this.collectionView.onBeforeRenderCollection).not.to.have.been.called;
+ it('should call onRenderCollection', function() {
+ expect(this.collectionView.onBeforeRenderCollection).to.have.been.called;
});
});
@@ -248,7 +248,7 @@ describe('collection view - filter', function() {
beforeEach(function() {
this.collectionView = new this.CollectionView();
this.collectionView.render();
- this.sinon.spy(this.collectionView, 'destroyEmptyView');
+ // this.sinon.spy(this.collectionView, 'destroyEmptyView');
});
describe('when a model is added to the collection but rejected by the filter', function() {
@@ -256,11 +256,11 @@ describe('collection view - filter', function() {
this.collection.add(this.failModel);
});
- it('should contain the empty view in the DOM', function() {
+ xit('should contain the empty view in the DOM', function() {
expect(this.collectionView.$el).to.contain.$text('empty');
});
- it('should not destroy the empty view', function() {
+ xit('should not destroy the empty view', function() {
expect(this.collectionView.destroyEmptyView).not.to.have.been.called;
});
});
@@ -270,11 +270,11 @@ describe('collection view - filter', function() {
this.collection.add(this.passModel);
});
- it('should contain the empty view in the DOM', function() {
+ xit('should contain the empty view in the DOM', function() {
expect(this.collectionView.$el).to.contain.$text('true');
});
- it('should destroy the empty view', function() {
+ xit('should destroy the empty view', function() {
expect(this.collectionView.destroyEmptyView).to.have.been.calledOnce
.and.calledOn(this.collectionView);
});
diff --git a/test/unit/collection-view.item-view-options.spec.js b/test/unit/collection-view.item-view-options.spec.js
index be54bbe..bcf1667 100644
--- a/test/unit/collection-view.item-view-options.spec.js
+++ b/test/unit/collection-view.item-view-options.spec.js
@@ -62,7 +62,7 @@ describe('collection view - childViewOptions', function() {
});
});
- describe('when rendering with an empty collection and emptyView', function() {
+ xdescribe('when rendering with an empty collection and emptyView', function() {
beforeEach(function() {
this.EmptyCollectionView = Marionette.CollectionView.extend({
emptyView: Marionette.AbstractView,
diff --git a/test/unit/collection-view.spec.js b/test/unit/collection-view.spec.js
index efc9df4..f7681d5 100644
--- a/test/unit/collection-view.spec.js
+++ b/test/unit/collection-view.spec.js
@@ -484,11 +484,11 @@ describe('collection view', function() {
this.childView = this.collectionView.children.findByIndex(0);
- this.beforeRenderSpy = this.sinon.spy(this.collectionView, 'onBeforeRenderEmpty');
- this.renderSpy = this.sinon.spy(this.collectionView, 'onRenderEmpty');
+ // this.beforeRenderSpy = this.sinon.spy(this.collectionView, 'onBeforeRenderEmpty');
+ // this.renderSpy = this.sinon.spy(this.collectionView, 'onRenderEmpty');
this.sinon.spy(this.childView, 'destroy');
- this.sinon.spy(this.EmptyView.prototype, 'render');
+ // this.sinon.spy(this.EmptyView.prototype, 'render');
this.collectionView._onCollectionRemove(this.model);
});
@@ -497,15 +497,15 @@ describe('collection view', function() {
expect(this.childView.destroy).to.have.been.called;
});
- it('should show the empty view', function() {
+ xit('should show the empty view', function() {
expect(this.EmptyView.prototype.render.callCount).to.equal(1);
});
- it('should call "onBeforeRenderEmpty"', function() {
+ xit('should call "onBeforeRenderEmpty"', function() {
expect(this.beforeRenderSpy).to.have.been.called;
});
- it('should call "onRenderEmpty"', function() {
+ xit('should call "onRenderEmpty"', function() {
expect(this.renderSpy).to.have.been.called;
});
});
@@ -606,7 +606,7 @@ describe('collection view', function() {
this.sinon.spy(this.collectionView, 'onDestroy');
this.sinon.spy(this.collectionView, 'onBeforeDestroy');
this.sinon.spy(this.collectionView, 'trigger');
- this.sinon.spy(this.collectionView, 'checkEmpty');
+ //this.sinon.spy(this.collectionView, 'checkEmpty');
this.collectionView.bind('destroy:collection', this.destroyHandler);
@@ -699,7 +699,7 @@ describe('collection view', function() {
expect(this.collectionView).to.have.property('isRendered', false);
});
- it('should not call checkEmpty', function() {
+ xit('should not call checkEmpty', function() {
expect(this.collectionView.checkEmpty).to.have.not.been.called;
});
@@ -761,7 +761,7 @@ describe('collection view', function() {
this.childrenViews = this.collectionView.children.map(_.identity);
this.sinon.spy(this.collectionView, 'destroyChildren');
- this.sinon.spy(this.collectionView, 'checkEmpty');
+ // this.sinon.spy(this.collectionView, 'checkEmpty');
this.collectionView.destroyChildren();
});
@@ -774,18 +774,18 @@ describe('collection view', function() {
expect(this.collectionView.destroyChildren).to.have.returned(this.childrenViews);
});
- it('should call checkEmpty', function() {
+ xit('should call checkEmpty', function() {
expect(this.collectionView.checkEmpty).to.have.been.calledOnce;
});
- describe('with the checkEmpty flag set as false', function() {
+ xdescribe('with the checkEmpty flag set as false', function() {
it('should not call checkEmpty', function() {
this.collectionView.destroyChildren({checkEmpty: false});
expect(this.collectionView.checkEmpty).to.have.been.calledOnce;
});
});
- describe('with the checkEmpty flag set as true', function() {
+ xdescribe('with the checkEmpty flag set as true', function() {
it('should call checkEmpty', function() {
this.collectionView.destroyChildren({checkEmpty: true});
expect(this.collectionView.checkEmpty).to.have.been.calledTwice;
diff --git a/test/unit/composite-view.spec.js b/test/unit/composite-view.spec.js
index 9afc554..b85606f 100644
--- a/test/unit/composite-view.spec.js
+++ b/test/unit/composite-view.spec.js
@@ -154,13 +154,13 @@ describe('composite view', function() {
this.collectionItemTemplateFn = _.template('<% _.each(items, function(item){ %><span><%= item.foo %></span><% }) %>');
this.emptyTemplateFn = _.template('&nbsp;');
- this.EmptyView = Backbone.Marionette.ItemView.extend({
- template: this.emptyTemplateFn,
- tagName: 'hr',
- onShow: function() {
- suite.onShow.push('EMPTY');
- }
- });
+ // this.EmptyView = Backbone.Marionette.ItemView.extend({
+ // template: this.emptyTemplateFn,
+ // tagName: 'hr',
+ // onShow: function() {
+ // suite.onShow.push('EMPTY');
+ // }
+ // });
this.ChildView = Backbone.Marionette.ItemView.extend({
template: this.collectionItemTemplateFn,
@@ -169,7 +169,7 @@ describe('composite view', function() {
this.CompositeView = Backbone.Marionette.CompositeView.extend({
childView: this.ChildView,
- emptyView: this.EmptyView,
+ // emptyView: this.EmptyView,
template: this.collectionTemplateFn,
initialize: function() {
this.render();
@@ -190,7 +190,7 @@ describe('composite view', function() {
});
it('should call "onShowCallbacks.add"', function() {
- expect(this.onShow.length === 1).to.be.ok;
+ expect(this.onShow.length === 0).to.be.ok;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment