ftlabs / fruitmachine

View rendering engine
MIT License
247 stars 18 forks source link

Collections #26

Closed wilsonpage closed 11 years ago

wilsonpage commented 11 years ago

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()
    }
  }
};
wilsonpage commented 11 years ago

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