AngularJS apps starten en stoppen

AngularJS apps starten en stoppen

Als je gebruik maakt van AngularJS componenten kan je er voor hebben gekozen dat elk component een nieuwe AngularJS app wordt. Je hebt dan nette en kleine componenten die je snel kunt inladen, maar toch kun je tegen problemen aanlopen. Als je bijvoorbeeld routing gebruikt kun je geen apps laden via de ng-app attribute. Routes zullen elkaar overschrijven waardoor apps onverwacht zullen reageren.

Memory leaks voorkomen

Met de app-manager kun je dynamisch een AngularJS app starten en een andere AngularJS app stoppen. Om memory leaks te voorkomen is het belangrijk dat alle scope listeners worden gestopt. Deze oplossing werkt alleen als je architectuur zo is opgezet dat twee componenten niet afhankelijk van elkaar zijn, in dat geval is het verstandig om de componenten samen te voegen in één app.

De appManager

const appManager = new function () {
    let currentAppName,
        currentApp;

    this.bootstrap = function (selector, appName) {
        if (currentApp) {
            this.destroyApp(currentApp);
        }
        var appContainer = document.querySelector(selector);
        if (appContainer) {
            currentAppName = appName;
            currentApp = angular.bootstrap(appContainer, [appName]);
        }
    }

    this.destroyApp = function (app) {
        const $rootScope = app.get('$rootScope');
        $rootScope.$destroy();
    }
}

appManager.bootstrap('body', 'app');
appManager.bootstrap('body', 'app2');

De appManager bestaat uit twee public methodes, bootstrap en destroy. Met bootstrap start je een app, met destory stop je een app. Bij het starten van de app wordt gecontroleerd of er een app actief is, deze wordt vervolgens gestopt om de nieuwe app te kunnen starten.

Plaats de appManager in de window zodat de appManager global beschikbaar is.

Reacties

Er zijn nog geen reacties, laat je reactie achter.

Laat een reactie achter

Lees ook

10 JavaScript one-liners every developer should know

Code should be readable and pragmatic. For me, that means when I look at a piece of code, I should almost instantly understand what it does. Clear code always has good variable names, is predictable, avoids unnecessary logic, and lives in the right place within a project. But sometimes you need to do something a little complex without turning the rest of your code into a mess. That’s where one-liners come in. Small, pragmatic snippets…

Continue reading

The difference between debounce and throttle

Debounce and throttle are powerful techniques, but only if you really understand what they do. I often ask about them in tech interviews, and to my surprise, many developers (even seniors) struggle to explain the difference. The good news is: it’s actually quite simple. In this post, I’ll break down both techniques and share some practical use cases. If you are unfamiliar with the techniques and are asked about them too in your interview…

Continue reading

The paradox of AI in web development

Since the start of my career as a developer, I’ve seen a lot of changes. From the rise of Dreamweaver with a WYSIWYG editor for generating table code to the arrival of jQuery and professional frameworks that completely transformed web development. Nowadays, there seems to be only one buzzword everywhere: AI.…

Continue reading