ftlabs / fruitmachine

View rendering engine
MIT License
247 stars 18 forks source link

How to refer parentData in child/slot template? #51

Closed sackIndian closed 11 years ago

sackIndian commented 11 years ago

My layout, var layout = { module: 'layout-a', parentData : 'my parent data', children: { 1: { module: 'masthead', model: { title: '

Article viewer

' } } } };

I pass a this layout to fruitmachine as, var view = fruitmachine(layout);

and masthead template if i want to get "parentData" ? var templateMasthead = Hogan.compile({{title}}-- parentData );

Thanks in advance..

wilsonpage commented 11 years ago

I think it's a bit of an 'anti-pattern' for a module to be printing its parent's data inside its own template. When you are using the 'children' loop to print all modules, we do make child model data available in the parent template. This means that if the parent module is a tab container and the children are modules that sit inside each tab, we can print the tab titles on the parent.

If you really, really need a key from the parent model I would simply advise sharing the same model between parent and child:

var model = new FruitMachine.Model({ shared: 'model' });

var view = fruitmachine({
  module: 'apple',
  model: model,
  children: {
    1: {
      module: 'pear',
      model: model
    }
  }
});

or duplicating the key onto the child model before you render:

var parent = new Apple({ model: { shared: 'model' } });
var child = new Pear();

child.model.set('shared', parent.model.get('shared'));

parent.add(child, { slot: 1 });

parent
  .render()
  .setup()
  .inject(document.body);
sackIndian commented 11 years ago

It is little bit tough process, in following layout structure it made me to create several models before creating a view and is difficult to achieve.

var sharedModel1 = new fruitmachine.Model({ shared: { parent : 'apple', font : 'arial' } });

var sharedModel2 = new fruitmachine.Model({ shared: { parent : 'banana', font : 'arial' } }); var layout = { module: 'layout-a', children: { 1: { module: 'apple', children: [ { module: 'masthead', model: { "model" : sharedModel1, "title" : "My title1", "pk_anrray" : "My arry" } }, { module: 'masthead', model: { "model" : sharedModel1, "title" : "My title1", "pk_anrray" : "My arry" } } ] }, 2: { module: 'banana', children: [ { module: 'masthead', model: { "model" : sharedModel2, "title" : "My title1", "pk_anrray" : "My arry" } }, { module: 'masthead', model: { "model" : sharedModel2, "title" : "My title1", "pk_anrray" : "My arry" } } ] } } };