user3084985
user3084985

Reputation: 41

Marionette collection view does not update when model added

I am new to Marionette. So may be its pretty basic.

I have a collection initialized globally and is empty during creation.

App.Colors= Backbone.Collection.extend({
    model : App.Color,
    comparator : "price"    
});


var appColors = new App.Colors();

If I create a Marionette collection view with this empty collection.

var colorview= new App.ColorView({collection:appColors});

and later on add to the collection appColors.

appColors.add({code:'red'})

Shouldn't the view be updated automatically since it listens to collection.add. I know it will work fine if I type:

colorview.collection.add({code:'red'})

Upvotes: 1

Views: 2383

Answers (1)

Rida BENHAMMANE
Rida BENHAMMANE

Reputation: 4129

In order for it to work, you must bind the view rendering to the collection add event, so basicly in your view declaration you must have something like this :

App.ColorView = Backbone.View.extend({
    initialize: function() {
        this.listenTo(this.collection, 'add', this.render);
        ...
    }
    ...
});

Upvotes: 1

Related Questions