I have a question regarding the recommended/support method of subscribing and unsubscribing to Durandal Publish "events". Looking at the code in event.js and closely reading its
api description, there are two methods of subscription/unsubscription that don't completely work when mixed:
[Strategy One]
Subscribe (1A)
var subscription = app.on(‘MyEventName’).then(OnMyEvent);
Unsubscribe (1B)
subscription.off();
[Strategy Two]
Subscribe (2A)
app.on(‘MyEventName’, OnMyEvent);
Unsubscribe (2B)
app.off(‘MyEventName’, OnMyEvent);
If you subscribe using 2A but unsubscribe using 1B (shown below) the result is that all event subscriptions on all events of app will be unsubscribed from. By observing the Durandal code, the object passed into the subscription variable is not a single subscription object but in fact set to app (intended for chaining purposes). Essential this is calling app.off() which unsubscribes to everything, usually not the desired result.
var subscription = app.on(‘incident:closed’, OnMyEvent);
...
subscription.off();
If you subscribe using 1A but unsubscribe using 2B (shown below) the event is correctly unsubscribed.
var subscription = app.on(‘MyEventName’).then(OnMyEvent);
...
app.off(‘incident:closed’, MyEventName);
Since Strategy One is the only method documented on the Pub/Sub documentation page, is there going to be continued support for the undocumented one? I think I would favor Strategy Two because it follows the jquery eventing conventions and because its less likely to inadvertently unsubscribe all events, but it will not be officially supported going forward it wouldn't be the right choice.