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):
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?
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.
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?
@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.)
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?
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.
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?