How to bind length from a backbone.Collection using StickIt?

141 views Asked by At

Using Backbone, Marionette (1.8.3), StickIt and TypeScript. How could I bind the lenght of a Backobone collection so that it gets update whenever items are added to or removed from the collection?

I'm quite new to StickIt and here what I have tried so far

export class SomeView extends marionette.CompositeView<backbone.Model, SomeItemView> {
(...)
bindings = {
    "[data-bind-observer=count]": {
        observe: ["collection.length"],
        onGet: (collection) => {
            return collection.length;
        }
    }
};

}

1

There are 1 answers

1
Rafael De Leon On

If you are using Backbone 1.3.3, it's better to listen to the collection's update event. But I am assuming you have to use an old version of Backbone because of work, so I'll give you a relevant answer

ui:{
  'count': '[data-bind-observer=count]'
},
collectionEvents: {
    add: "updateCount",
    remove: "updateCount"
},
updateCount: function() {
   this.ui.count.text(this.collection.length);
},

If you can, try upgrading to Backbone 1.3.3 or Marionette 3.