cerebral-legacy / cerebral-view-react

React View layer package for Cerebral
16 stars 9 forks source link

Support modules #38

Closed garth closed 8 years ago

garth commented 8 years ago

Companion to https://github.com/cerebral/cerebral/pull/149

enables the following:

@Cerebral({
})
export default class Application extends Component {
  static propTypes = {
    modules: PropTypes.object,
    signals: PropTypes.object
  };

  render() {
    const Home = this.props.modules.home.Component;

    return (
      <Home/>
    );
  }
}
christianalfoni commented 8 years ago

Awesome :-)

Guria commented 8 years ago

Main issue is that component can't be sure which module name was used to register

Guria commented 8 years ago

Another one: I can't see a difference between controller.services.moduleName.service() and controller.modules.moduleName.services.service() why to dublicate?

garth commented 8 years ago

I think that controller.services.moduleName.service() is the important one. The other is only there as a way for the module to export it's services, but could be removed.

The primary goal was to be able to use module services from any action.

function someAction({ services: { moduleName } }) {
  moduleName.service();
}

I may want to reuse a service twice, eg multiple db's, so the unique moduleName given at module setup is important.

To access the given name you can use a factory, but with a factory then you can just access the other properties of the module directly also, so the name is then less important.

import Component from './components';
{
  init({ controller, name }) {
    return {
      Component: Component(controller.modules[name])
  }
}
export default function (module) {
  class ModuleComponent extends Component {
    render() {
      <Link signal={module.signals.clicked}>Send namespaced module signal</Link>
    }
  }

  return ModuleComponent;
}