0
votes

I have a LayoutView that consists of two regions. These two regions share the same collection/collection-view, the only difference being the API endpoint that the collection calls to.

 initialize: function () {
     // setup collection for scheduled mailings
     this._scheduledView = new MailingsCollectionView({
         collection: new MailingsCollection()
     });
     this._scheduledView.collection.url = '/api/mailings?is_scheduled=true&mailing_types=m';

     // setup collection for sent mailings
     this._sentView = new MailingsCollectionView({
         collection: new MailingsCollection()
     });
     this._sentView.collection.url = '/api/mailings?mailing_statuses=c&mailing_types=m';

     this.listenTo(this._scheduledView.collection, 'change:checked', this.setMailing)
 },

Instead of writing the this.listenTo() line for each region, how can I listenTo the shared collection at one time?

1

1 Answers

0
votes

There is no way to to listenTo multiple objects, however you could break some logic out to make it just as clean as a single listenTo call.


initialize: function()
{
  this._scheduledView = this.makeCollectionView('/api/mailings?is_scheduled=true&mailing_types=m');
  this._sentView = this.makeCollectionView('/api/mailings?mailing_statuses=c&mailing_types=m');
},

makeCollectionView: function(url)
{
  var collection = new MailingsCollection({url: url}),
      collectionView = new MailingsCollectionView({collection: collection});

  this.listenTo(collection, 'change:checked', this.setMailing);

  return collectionView;
}