Question

Our durandal application has many modular UI components (all created using AMD constructor based viewmodels) and I would like to begin adding pub/sub communications. I need to determine the best way to ensure that subscriptions are 'cleaned up' properly.

When/how should these subscriptions be cleaned up?

The deactivate event seems to be sufficient for a select few viewmodels, but what about the rest? I don't see a particular mechanism which would allow a viewmodel to reliably 'clean up' after itself.

Before I go implementing some sort of disposable pattern, is there some obvious approach I'm missing here?

Followup question: Is there anything to be done other than calling subscription.off() to properly clean up?

Was it helpful?

Solution

Calling subscription.off() from deactivate when working with view models or from detached when working with widgets should be all that's required. The event system has a bunch of test cases https://github.com/BlueSpire/Durandal/blob/master/test/specs/events.spec.js#L240, which could be used as starter for your own testing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top