Skip to content

Instantly share code, notes, and snippets.

@christianalfoni
Created April 21, 2016 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianalfoni/34775b1274263b2f5289ac9ff4403b28 to your computer and use it in GitHub Desktop.
Save christianalfoni/34775b1274263b2f5289ac9ff4403b28 to your computer and use it in GitHub Desktop.
function ModulesProvider(context, execution, controller) {
var modules = controller.getModules();
var services = controller.getServices();
context.modules = Object.keys(modules).reduce(function (contextModules, key) {
var modulePath = key.split('.');
var module = modulePath.reduce(function (contextModules, pathKey) {
contextModules[pathKey] = contextModules[pathKey] || {};
return contextModules[pathKey];
}, contextModules);
module.meta = modules[key];
module.state = context.state.select(modulePath);
module.services = modulePath.reduce(function (services, key) {
return services[key] || {};
}, services);
if (
execution.options.modulePath &&
execution.options.modulePath.join('.') === key) {
context.module = module;
}
return contextModules;
}, {});
return context;
}
function SomeAction({module, modules}) {
module.state.set('foo', 'bar');
module.services.foo();
module.meta // {path,name, ...}
modules.someOtherModule.state('foo', 'bar');
modules.someOtherModule.services.foo();
modules.someOtherModule.meta // {path,name, ...}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment