ftlabs / fruitmachine

View rendering engine
MIT License
247 stars 18 forks source link

Model collections? #61

Closed tomgp closed 10 years ago

tomgp commented 10 years ago
  1. Do you have any suggestions for (or examples of) dealing with models which contain models? i.e. the kind of situation which backbone's Collections are designed to deal with. So you can put a listener for 'change' events on the top level model collection and changes to sub-models will trigger that event.
  2. What do you think of the idea of using Backbone models and collections with fruitmachine for managing the views.
wilsonpage commented 10 years ago

You should be able to use Backbone Models and Collections if you wish. The only requirement is that the 'Model' you assign to a view has a .toJSON method (which I believe Backbone collections do). FruitMachine will call this method just before templating, and pass the output as the first parameter to your template function.

var Apple = fruitmachine.define({
  name: 'apple',
  template: function(json) {
    return '<h1>' + json.title + '</h1>'
  }
});

var model = new backbone.Model({ title: 'hello world' });
var apple = new Apple({ model: model });

apple.render();
apple.el.innerHTML; //=> '<h1>hello world</h1>';
tomgp commented 10 years ago

OK, that seems reasonable, thanks.