2
votes

I have a Marionette Module with several Sub Modules. The parent Module has its own event aggregator that I would like to use in the sub modules to trigger events. I know that I could use the Application's event aggregator, but these events are specific to the Parent Module and its Children, and not the whole Application.

I could namespace the events in the Application's event aggregator like so:

App.module("Parent.Child", function(self, App, ...) {

  // somewhere in the child
  App.vent.trigger("Parent:something");
});

But I'd really rather not go that route. I think the idea of having a single event aggregator for Parent Module and its children is cleaner. I like having a single interface from the Parent to the Application, and the Child to the Parent ... but perhaps I'm wrong in this thinking?

I could also get to the Parent Module's event aggregator from the App object like so:

App.module("Parent.Child", function(self, App, ...) {

  // somewhere in the child...
  App.Parent.vent.trigger("something");
});

But I'd rather not do that either. I think that would couple the Child module and the App too tightly.

Any other idea's or options? Maybe these are good ideas, and I just don't understand the benefits.

2
instead of using events, I just called App.Parent.something() from the child modulegalki

2 Answers

8
votes

Unfortunately, while Marionette gives you the ability to drill down the app/module/submodule chain via the submodules property, it doesn't offer easy access to identify a Module's parent. We've encountered a few times where this could have been helpful, but never ran into a situation where not having it became an issue. That said, if you think it'll make your codebase cleaner you might try wrapping the _addModuleDefinition function to create a parent property:

var func = Marionette.Module._addModuleDefinition;
Marionette.Module._addModuleDefinition = function(parentModule, module) {
    module.parent = parentModule;
    func.apply(this, arguments);
};

This would give you the ability to do something like

App.module("Parent.Child", function(self, App, ...) {
    self.parent.trigger('whatever'); // (vent isn't required anymore)
});
-1
votes

You could substitute your parent module with subapp. This way you can use subapp events.