rniemeyer / knockout-postbox

A small library that uses Knockout's native pub/sub capabilities to facilitate decoupled communication between separate view models or components.
MIT License
350 stars 55 forks source link

Clear subscriptions programatically #25

Closed darrenhaken closed 9 years ago

darrenhaken commented 10 years ago

Is it possible to clear subscriptions programatically?

I'm using Jasmine to write unit tests and I've noticed events seem to be firing in unexpected ways across tests. It would be good to clear all subscriptions as a clear up between tests as a guard to this.

I've seen another approach mentioned in an issue where they manually unsubscribe on each topic. However comes with risk as an unsubscribe could be missed, not to mention the overhead of managing each subscription.

rniemeyer commented 10 years ago

@darrenhaken - this sounds like something that I could explore adding. do you typically use ko.postbox directly or the observable extensions?

darrenhaken commented 10 years ago

I've managed to work around this for now by doing the following in the tear down of the tests:

ko.postbox.H = {}

Can you see any problems with this?

rniemeyer commented 10 years ago

The only issue is that H will likely be minified to a different name in each build of Knockout, so it would not be the same between KO versions.

darrenhaken commented 10 years ago

Hi Ryan,

I think it would be useful to have this feature built into the library then.

I'm happy to work on this with you if you would like me to?

Thanks,

Darren

On 29 May 2014 16:14, Ryan Niemeyer notifications@github.com wrote:

The only issue is that H will likely be minified to a different name in each build of Knockout, so it would not be the same between KO versions.

— Reply to this email directly or view it on GitHub https://github.com/rniemeyer/knockout-postbox/issues/25#issuecomment-44543074 .

rniemeyer commented 10 years ago

@darrenhaken - if you have some time to try to implement it, then feel free to give it a shot and/or to use this thread to post ideas/questions/thoughts about it. Thanks!

ffMathy commented 9 years ago

This is incredibly important to me as well. My tests are failing constantly, and I have to arrange them differently for them to magically just work.

It is very critical.

@darrenhaken what is the H variable called in the debug version of Postbox?

ffMathy commented 9 years ago

Oh, and furthermore, it also breaks my single-page design. If I use Pjax for single-page navigation, I would like to be able to clear my subscriptions when Pjax "navigates" to a new page without changing the URL.

ffMathy commented 9 years ago

I created a pull request which adds a unit test for this, and also a method to clear all variables. @rniemeyer could you check it out?

rniemeyer commented 9 years ago

@ffMathy - Thanks! I'll take a look when I have a chance.

rniemeyer commented 9 years ago

There is now a reset method in v0.5.0 that will dispose all subscriptions related to ko.postbox functionality.