ftlabs / fruitmachine

View rendering engine
MIT License
247 stars 18 forks source link

Alternative way of handling children and slots #24

Closed wilsonpage closed 11 years ago

wilsonpage commented 11 years ago

Does it make sense for child modules to decide their own location? This seems to go against our concept of top down modularity.

var layout = new Layout({
  children: [
    {
      module: 'apple',
      slot: 1
    },
    {
      module: 'orange',
      slot: 2
    }
  ]
});

could become...

// lazy
var layout = new Layout({
  children: {
    1: { module: 'apple' },
    2: { module: 'orange' }
  }
});

// explicit
var layout = new Layout();
var apple = new Apple();
var orange = new Orange();

layout
  .add({ 1: apple })
  .add({ 2: orange });

Now modules are no longer aware of their location. layout.children is no longer an array, but an object. Keys mapping directly to slots on that view.

Again, just a thought I wanted to to get down for future discussion.

wilsonpage commented 11 years ago
layout
  .set(1, orange)
  .set(2, apple);
wilsonpage commented 11 years ago
layout
  .slot(1, orange)
  .slot(2, apple);
wilsonpage commented 11 years ago
layout
  .add(orange)
  .add(apple);
wilsonpage commented 11 years ago

Each module could store a reference to it's slot but the canonical reference would be held on the parent.

That way when we do apple.destroy() we can still trace back the reference on the parent so that it can be removed.

wilsonpage commented 11 years ago

It is probably sensible to support both methods. I think to build in the support for this each parent must also have a hash of children by slot.

layout._childhash;
//=> { 1: apple, 2: orange }

layout.children;
//=> [ apple, orange ]

Adding:

var apple = new Apple({ slot: 1 });
layout.add(apple);

// same as

var apple = new Apple();
layout.slot(1, apple);

// same as

var layout = new Layout({
  children: [
    {
      module: 'apple',
      slot: 1
    }
  ]
});

// same as

var layout = new Layout({
  children: {
    1: {
      module: 'apple'
    }
  }
});

We have to keep the add method to support 'list' layouts as well as 'slot' layouts. This is because list layouts (think TODO list) don't have slots, they jsut need to print all the children in the correct order.

wilsonpage commented 11 years ago

This has been implemented in a backwards compatible way. We need to update the docs to show how slots should be used.