0

Can some advise what the best approach is for listening for the sync event on my collection. Should it be on or listenTo, kind of confused by what's right.

this.collection.on('sync', this.render, this);

// or

this.listenTo(this.collection, 'sync', this.render);
styler
  • 15,779
  • 23
  • 81
  • 135
  • It's explained in the [docs](http://backbonejs.org/#Events-listenTo) – ivarni Jan 12 '16 at 10:24
  • Hey, yeah I'm aware of the docs; confused as which one I should use though. Should I just use listenTo all the time since it handles cleaning itself up instead of having to manage `on` and the `off`? – styler Jan 12 '16 at 10:26
  • 1
    Yes, use `listenTo` unless you want to manage the cleaning manually. I suppose if for some reason you want to re-use listeners then `on` or `off` is the way to go but I can't really think of any real life situation where `listenTo` won't be a better choice. – ivarni Jan 12 '16 at 10:28
  • Do note that it's not automagic, you or the library need to call [stopListening](http://backbonejs.org/#Events-stopListening) which Backbone will do in a view's [remove](http://backbonejs.org/#View-remove). – ivarni Jan 12 '16 at 10:29

0 Answers0