Aurelia component with no view: A bad idea?

In order to “separate the concerns” in the logic of one of my components, I want to create an object that is only meant to listen to some events and compute a field accordingly.

My problem is that it needs to unsubscribe from the listening when disposed. To do that, I’m using detached(). It seems to work.

But is it a good idea? This so-called component has no view. It acts more like a proxy service. But it does rely on Aurelia’s “@computedFrom”.

So… Is there another way? Is it horrifying?

@autoinject
export class MyListener implements ComponentAttached, ComponentDetached {

    // @ts-ignore
    // This value has no meaning in itself; it is used to trigger recalculations
    private mToggle: boolean = false;

    constructor(private mEventSubscriber: EventSubscriber) {
        this.mEventSubscriber.subscribe(Events.SOMETHING_CHANGED, () => {
            this.mToggle = !this.mToggle;
        });
    }


    @computedFrom("mToggle")
    get someImportantValue(): boolean {
        return someComplicatedCalculations(...);
    }

    public detached(): void {
        this.mEventSubscriber.removeAll();
    }
}

Interesting. Maybe as an alternative approach you could try using a custom attribute?

I have no idea what I’m looking at.
First of all, I don’t see anything on the page? I sthere supposed to be a live demo?

But seriously though.

I think it would make more sense to just use @observable (or something similar, i.e. something more generic).

What I really want to know is : what are the lifecycle methods into why I should initiate (and later destroy) the listening, since I’m not working with a “real” Aurelia component (i.e. it has no view).

Surely there’s something better than attach and detach.

Sorry wrong link. I updated it :grinning: