ftlabs / fruitmachine

View rendering engine
MIT License
247 stars 18 forks source link

Inflation hooks #29

Closed wilsonpage closed 11 years ago

wilsonpage commented 11 years ago

So I made a workaround for collections that I think works pretty well using a third party collections library. Although it lead me to think that we may run into difficulties when inflating a View on the client that is heavily dependent on third party stuff and not on fruitmachine core.

Solution

New hooks via the event model can help us here. When a view is using a collection it will probably be stored like myview.collection. When we call myview.toJSON() to send down to the client with the html, what happens to myview.collection? It gets lost!

But if we fire a new event 'tojson' we can ensure that extra data can be bolted on as and when needed.

myview.on('tojson', function() {
  this.json.collection = this.collection;
});

myview.toJSON()
//=> { ... collection: {}, ...}

We would also need a second event that would be fired on the client 'inflation'. That would fire on each view when it is inflated from the server.

myview.on('inflation', function(options) {
  // ... do something with options.collection
});

What does this all enable?

This basically empowers FruitMachine helpers to interviene at every stage from server to client. Getting involved in the toJSON() and 'inflation' events. My collection example could then become a helper, seeing that collections safely arrive on the client.

wilsonpage commented 11 years ago

Added here dd5c2f47d4254d42709cd9d46c609cd461609e03