ftlabs/fruitmachine

Collections

wilsonpage opened this issue · 1 comments

How do we cope with views that represent collections/lists of models?

The banana module for example can hold 3 stories. Each one of these stories is represented by an article model.

FruitMachine is designed to stay away from your data as much as possible. The only thing it needs to know about is the model associated with each view in order to extract the data for templating.

var Banana = fm.define({
  module: 'banana',
  initialize: function(options) {
    var collection = options.collection;

    collection.forEach(this.addItem);
    collection.on('add', this.render);
  },

  addItem: function(item) {
    this.add(new Plum({ model: item }));
  }
});

var collection = getCollection();
var banana = new Banana({ collection: collection });

The collection could alternatively be defined in layout JSON:

var layout = {
  module: 'layout-a',
  children: {
    1: {
      module: 'banana',
      collection: getCollection()
    }
  }
};

This example (4600e24) demonstrates how developers can incorporate collections into their views abstracted away from FruitMachine.