Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a backbone view for a collection

Tags:

backbone.js

How can I bind a backbone view to a collection rather than a model? Do I need to wrap the collection in a model?

e.g.

If I have a backbone model Client and a collection of these called Clients

Client = Backbone.Model.extend({
    defaults: {
        Name: ''
    }
});

Clients = Backbone.Collection.extend({
    model: Client,
    url: 'Clients'
});

and a view

    var ClientListView = Backbone.View.extend({
        template: _.template($("#clients-template").html()),
        el: $('#clientlist'),

        initialize: function() {
            _.bindAll(this, 'render');

            this.collection = new Clients();
        },

        render: function( event ){
            $(this.el).html(this.template({ this.collection.toJSON()));

            return this;
        }
    });

then I can't access each client element in the underscore template. However if I wrap the collection like this

$(this.el).html(this.template({ clients: this.collection.toJSON() }));

then I can. Is this the correct way to go about this? I would expect this to be a common scenario but I can't find any examples on it, am I going about it the wrong way?

like image 251
Chris Herring Avatar asked Jan 13 '12 00:01

Chris Herring


1 Answers

Yes, you need to pass the wrapped collection.

Addy Osmani is using similar approach in his Backbone Fundamentals examples - see for example this view and corresponding template:

In the view:

$el.html( compiled_template( { results: collection.models } ) );

In the template:

<% _.each( results, function( item, i ){ %>
   ...
<% }); %>

Another alternative is to have a view that will create separate view for each model in the collection. Here is an example from An Intro to Backbone.js: Part 3 – Binding a Collection to a View:

var DonutCollectionView = Backbone.View.extend({
  initialize : function() {
    this._donutViews = [];
    this.collection.each(function(donut) {
      that._donutViews.push(new UpdatingDonutView({
        model : donut,
        tagName : 'li'
      }));
    });
  },

  render : function() {
    var that = this;
    $(this.el).empty();

    _(this._donutViews).each(function(dv) {
      $(that.el).append(dv.render().el);
    });
  }
});
like image 75
kubetz Avatar answered Oct 04 '22 23:10

kubetz