Marionette LayoutView地区共享CollectionView

时间:2015-02-20 19:20:16

标签: javascript backbone.js marionette

我有一个LayoutView,由两个区域组成。这两个区域共享相同的集合/集合视图,唯一的区别是集合调用的API端点。

 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)
 },

不是为每个区域编写this.listenTo()行,而是如何一次收听共享收藏?

1 个答案:

答案 0 :(得分:0)

没有办法listenTo多个对象,但是你可以打破一些逻辑,使其像单个listenTo调用一样干净。


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