1

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;
        }
    }
};

}

TTT
  • 1,848
  • 2
  • 30
  • 60

1 Answers1

3

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.

Rafael De Leon
  • 1,580
  • 1
  • 12
  • 11
  • From what I see, we're using Backbone 1.1.0. And the whole company is stuck with those old versions, I can't do anything about it. My initial code was more or less right but what I was missing was a company-built Marionette behaviour that would start StickIt. So I can't really test it now but I see something interesting about Marionette in you post. Thanks. – TTT Nov 22 '17 at 09:05