rotundasoftware / backbone.collectionView

Easily render backbone.js collections. In addition to managing model views, this class supports automatic selection of models in response to clicks, reordering models via drag and drop, and more.
http://rotundasoftware.github.io/backbone.collectionView/
Other
171 stars 27 forks source link

Backbone.CollectionView

For demos see the Backbone.CollectionView home page.

Depends on jQuery and jQueryUI for event handling and sorting, respectively.

Benefits

Sample Usage

var myCollectionView = new Backbone.CollectionView( {
    el : $( "#listForCollection" ),     // must be a 'ul' (i.e. unordered list) or 'table' element
    modelView : EmployeeView,           // a View class to be used for rendering each model in the collection
    collection : employeeCollection
} );

myCollectionView.render();
myCollectionView.setSelectedModel( employeeCollection.first() );

Initialization Options

The following options apply when selectable option is set:

The following options expect a filter function that takes a single parameter, the model in question, and returns true or false. They are all optional, defaulting to passing all models.

Methods and Properties Reference

setSelectedModel(s) and getSelectedModel(s)

The getSelectedModel(s) and setSelectedModel(s) methods are used to get or set the currently selected models. The methods are able to reference models in a variety of ways using the by option:

// Returns an array of the selected models
myCollectionView.getSelectedModels();

// Returns an array of the ids of the selected models
myCollectionView.getSelectedModels( { by : "id" } );

// Select model id 2 and model id 4
myCollectionView.setSelectedModels( [ 2, 4 ], { by : "id" } );

// Select the model with cid equal to "c21"
myCollectionView.setSelectedModel( "c21", { by : "cid" } );

// Returns the view object that represents the selected model
myCollectionView.getSelectedModel( { by : "view" } );

As shown in the examples, the plural versions of the methods expect / return an array of "model references", whereas the singular versions expect / return just a single "model reference".

There are four valid values for by option which determine the type of "model reference" used.

If no by option is provided the model object itself is expected / returned.

Additionally, the setSelectedModel(s) function accepts one additional option, silent, which, when true, will prevent the selectionChanged event from being fired.

Events Fired

CollectionViews trigger the following events on themselves. If Backbone.Courier is available, these events are also "spawned".

In addition, sortable CollectionViews fire these events:

Styling

How you style the collection view is up to you.

The ul or table element that is used as the collection view's element will be given the collection-list class. Generally you will want to eliminate bullets, etc., from your collection view list elements, which you can do with these "base" styles:

ul.collection-list {
  margin: 0;
  padding: 0;
  list-style-type: none;
  outline: none;
  cursor: pointer;
}

When a model is selected, its view's li or tr element will be given the selected class.

You can style the caption created by the emptyListCaption option with the var.empty-list-caption selector. These styles will center the empty list caption text near the top of the collection view.

var.empty-list-caption {
  color: #A0A0A0;
  padding: 30px;
  display: block;
  text-align: center;
  font-style: normal;
  line-height: 1.45;
}

(Both of the above css fragments are included in base.css, which will be included automatically in your css bundles if you are using parcelify or cartero.)

See the the Backbone.CollectionView home page for styling examples.

Contributing

See the the Backbone.CollectionView home page for styling examples.

Dependencies

License

MIT