ftlabs/fruitmachine

Alternative way of handling children and slots

wilsonpage opened this issue · 6 comments

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.

layout
  .set(1, orange)
  .set(2, apple);
layout
  .slot(1, orange)
  .slot(2, apple);
layout
  .add(orange)
  .add(apple);

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.

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.

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