# Making the eventaggregator global across enhanced aurelia roots

**URL:** <https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311>\
**Category:** vNext\
**Created:** [July 9, 2021, 12:02pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311 "2021-07-09T12:02:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [July 9, 2021, 12:02pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/1 "2021-07-09T12:02:35Z")

</div>

I’ve started looking a Au2 from the perspective of an Aurelia 1 application that relies heavily on the “enhance” feature of Aurelia.

I noticed in the docs that each enhanced element now gets its own DI container and I noticed that when using the EventAggregator a subscription created in one container would not fire when the event is published from another container (ie in an enhanced element).

To revert back to the more global behavior I have in the Aurelia 1 application I did the following in my main startup, where `au` is the “main” aurelia instance and `registrations` is an array where I store all global registrations (such as custom elements etc):

```auto
let globalEventAggregator = au.container.get(EventAggregator);
registrations.push(Registration.instance(EventAggregator, globalEventAggregator));

```

The `registrations` collection is then reused each time I enhance any other element within the application, so now my event publishing across different aurelia instances/DI containers works as I need it.

Is this a fine way to do it or am I doing something crazy here?

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [July 9, 2021, 12:17pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/2 "2021-07-09T12:17:25Z")

</div>

The enhance story is incomplete at the moment in v2, as it works as if it’s a whole new app. We will bring back the v1 behavior, and existing container can be reused for view enhancement.

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [July 9, 2021, 12:22pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/3 "2021-07-09T12:22:35Z")

</div>

Ok, good to know. Perhaps I should await that before I continue my investigations further. Do you have any ETA for when enhance will be more “as intended” or should I just follow the blog? 🙂

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [July 9, 2021, 10:46pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/4 "2021-07-09T22:46:58Z")

</div>

We are at alpha 10, and about to get to alpha 11. It probably will be in around ~13 (~2 weeks from now)

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [July 18, 2021, 4:08am UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/5 "2021-07-18T04:08:04Z")

</div>

It should be covered by this PR [https://github.com/aurelia/aurelia/pull/1247](https://github.com/aurelia/aurelia/pull/1247)  
The usage would be similar to v1:

```auto
aureliaInstance.enhance({
  host: someElement,
  component: someObject_or_someConstructor,
  container: container_for_the_custom_element_of_the_enhancement
})

```

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [July 19, 2021, 12:07pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/6 "2021-07-19T12:07:13Z")

</div>

@bigopon  
I’m trying it out but it seems to cause some problems with the routing giving me this error message;

> A root RouteContext is already registered. A possible cause is the RouterConfiguration being registered more than once in the same container tree. If you have a multi-rooted app, make sure you register RouterConfiguration only in the “forked” containers and not in the common root.

I’m not really sure how to set up the routing and where to put the au-viewport if I can’t do it in the component sent to “app” in my main startup.

Also, the `container` option in your example above is not allowed with the current type definitions, but forcing it to be set anyway does not seem to solve the above issue. What is it set to if omitted, would it be `aureliaInstance.container`, given your example?

(Perhaps it’s a bit uncommon with routing and usage of enhance in the same app, but for my case the “routing components” I have actually fetches server side generated html from the server, that is then enhanced.)

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [July 19, 2021, 12:10pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/7 "2021-07-19T12:10:03Z")

</div>

normally you wouldn’t need to give a container yourself, since a new one will be created if no `container` given. If you do specify one, it’s preferably a new container. Can you show some example code of what you are doing?

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [July 19, 2021, 12:29pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/8 "2021-07-19T12:29:07Z")

</div>

Here is the repo for my Aurelia 2 Proof Of Concept-app;

> **[deap82/aspnetcoremvc-razorfirst-with-aurelia2](https://github.com/deap82/aspnetcoremvc-razorfirst-with-aurelia2)**
>
> My "razor first" approach combined with Aurelia 2. - deap82/aspnetcoremvc-razorfirst-with-aurelia2

This PR contains the changes I tried with today;

> <https://github.com/deap82/aspnetcoremvc-razorfirst-with-aurelia2/pull/1>
>
> Trying to use existing aurelia instance for enhancement, instead of creating new… instances.

If it’s to much to digest, just say so and I’ll try to explain it further.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [July 19, 2021, 12:45pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/9 "2021-07-19T12:45:07Z")

</div>

In the package-lock, it seems the version of Aurelia is resolved to alpha-10. Can you update it to latest, which is alpha-13? The “new” enhance API is only out in alpha-13.  
I’ve also added a comment in the PR above.

Here’s the doc for our enhance API [App configuration and startup - The Aurelia Docs](https://docs.aurelia.io/getting-to-know-aurelia/app-configuration-and-startup#enhance)

Can you have a check?

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [July 21, 2021, 11:45am UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/10 "2021-07-21T11:45:46Z")

</div>

Yeah, that was weird with the versioning stuff, i got 12 when I did npm update.

Anyway, after ensuring alpha 13, my routing is still broken so I can’t really verify if enhance works or not. When I click a navigation link I get;  
 ![image](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/d/d407283b3a29e255c56b91e3bb8f6f9b0ac46fa5.png)

When I try to look att controller.ts:204, I get

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/0/0c4c7252d61e22128ac3d63e3f0daeedba23c0d3.png)

It’s as if the controller.ts file hasn’t been included in the entry-bundle…?

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [July 21, 2021, 12:00pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/11 "2021-07-21T12:00:08Z")

</div>

That means sourcemap isn’t working for your local development. I’m not sure why. For your error, can you show the code of how you are hooking into `canLoad`?

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [July 21, 2021, 12:32pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/12 "2021-07-21T12:32:07Z")

</div>

I have no canLoad implementation, this is the router component for the single generic route rule set up;

> <https://github.com/deap82/aspnetcoremvc-razorfirst-with-aurelia2/blob/alpha-13-enhance/src/FooBarFrontend/common/routing/mvc-route-navigation/mvc-route.ts>

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [July 21, 2021, 1:19pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/13 "2021-07-21T13:19:50Z")

</div>

I’m not sure about this issue. I’ll try to replicate it or if you could help create a small repro, it would be of great help!

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [July 22, 2021, 1:53pm UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/14 "2021-07-22T13:53:04Z")

</div>

Vacation times are here for me so I’ll have to pause it for now, will get back to you here later on if needed. Thanks for all the help.

---

<div class="post-metadata">

**Author:** ![deap82](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/deap82/32/203_2.png) [@deap82](https://discourse.aurelia.io/u/deap82)\
**Post date:** [March 13, 2022, 11:50am UTC](https://discourse.aurelia.io/t/making-the-eventaggregator-global-across-enhanced-aurelia-roots/4311/16 "2022-03-13T11:50:56Z")

</div>

Sorry, I made a mistake when posting here.

The routing issues seems resolved when using aurelia@2.0.0-alpha.25.

The event aggregator issues is resolved by using the aurelia instance returned from Aurelia.register when doing enhance.
