Events not firing in --release --prod mode

I’d recommend starting a new thread with more information and also the output of ionic info.

Production mode runs uglifyjs, which changes all your class names. Never rely on class names in your code.

1 Like

A simple way of keeping track of active pages without using component names is to create your own singleton observer class – really useful design pattern combination.

We use the singleton pattern, because we only want one instance of the class over the entirety of the application.

class ActivePageObserver {
    private static instance : ActivePageObserver = null;

    private handlers : any[] = [];
    public addHandler(handler : any) : void {
        this.handlers.push(handler);
    }
    public getHandlers() : any[] {
        return this.handlers;
    }

    public static getInstance() : ActivePageObserver {
        if (this.instance == null) {
            this.instance = new ActivePageObserver();
        }
        return this.instance;
    }

    public static listen(callbackFn) : void {
        this.getInstance().addHandler(callbackFn);
    }

    public static notify(pageName : string) : void {
        let handlers : any[] = this.getInstance.getHandlers();
        for (let handler of handlers) {
            handler(pageName);
        }
    }
};

// in the app component (or where the menu is controlled)
ActivePageObserver.listen((newPage) => {
    this.activePage = newPage;
});

// in pages:
ionViewDidLoad() {
    ActivePageObserver.notify('MyPageName');
}

Take the code above with a handful of salt. I haven’t tested it.

While it’s not a bad design pattern in and of itself, the proposed singleton in the previous post is a massive antipattern in an Angular app. Angular gives us a DI system, use it. Do not go rogue and try to manage object lifecycle by yourself.

Overall, it’s also a red flag to me when people want to know what the current page is. I don’t think I’ve ever seen a situation where that was really the most clean, maintainable way to achieve the overarching goal.

Just throwing this here:

If you want to check to see if a page exists on your stack before adding it, this is the best approach:

private inPages(page:any):boolean {
    return this.navController.indexOf(page) >= 0;
}

My error was checking each page by name when I should have been using the built-in navController indexOf method.