derickbailey/backbone.marionette — pull request #126 by beardtwizzle (2012-06-06 15:42:45+00:00)
Title: Optional 'empty view', when CollectionView has an empty collection

Background
====

The current behaviour of a `CollectionView` is to either render all items in a collection, or nothing at all.

Whilst this is completely understandable, we've found that in our applications when encountering an empty collection (especially as a result of a `fetch` from the server), we want to be able to render a placeholder such as "no results found".

What's included
====

This pull request makes some minor changes (and associated spec updates), that add the ability to **optionally** include an `emptyView` parameter to any collection view.

When the collection's `render` function is called, if the collection length is 0, then the `emptyView` will be appended to the collection view, if one has been specified.

Sample usage
====

var ItemView = Backbone.Marionette.ItemView.extend({
...
});

var EmptyView = Backbone.Marionette.ItemView.extend({
...
});

var CollectionView = Backbone.Marionette.CollectionView.extend({
// Will result in one or more renders of `itemView` if collection has items
itemView: ItemView,

// [optional] - if not here, normal behaviour will result
// Will result in a single render of `emptyView` if collection has 0 items
emptyView: EmptyView
});