Skip to content Skip to sidebar Skip to footer

Backbone JS Template Printing Only Last Item In The Collection

I am facing a problem with my backbone view. I am trying to render an array of day objects and each day object contains another array of time objects. I get the collection from my

Solution 1:

When fetching data in a collection, Backbone will perform a collection.set with the data:

set collection.set(models, [options])
The set method performs a "smart" update of the collection with the passed list of models. If a model in the list isn't yet in the collection it will be added; if the model is already in the collection its attributes will be merged; and if the collection contains any models that aren't present in the list, they'll be removed.

All your models have the same id, 0, which seems to hopelessly confuse Backbone : see this demo http://jsfiddle.net/T3fmx/

To solve your quandary, you can either:

  • remove the id attribute from the server response or assign unique values, http://jsfiddle.net/T3fmx/3/

  • set a fake idAttribute in your model : http://jsfiddle.net/T3fmx/1/

    kronos.Slot = Backbone.Model.extend({
        idAttribute: 'whatever'
    });
    
  • parse the data returned to omit the id attribute : http://jsfiddle.net/T3fmx/2/

    kronos.Slot = Backbone.Model.extend({
        parse: function(data) {
            return _.omit(data, 'id');
        }
    });
    

Post a Comment for "Backbone JS Template Printing Only Last Item In The Collection"