ManuelDeLeon / viewmodel

MVVM for Meteor
https://viewmodel.org
MIT License
205 stars 23 forks source link

Subscriptions inside onCreated don't unsubscribe automatically after viewmodel is destroyed #270

Closed tomyo closed 8 years ago

tomyo commented 8 years ago

When clicking to somewhere else in the site (flowrouter), subscription inside onCreated method don't unsubscribe. If I use autorun(), works as expected (unsubscribes automatically after viewmodel is destroyed). Looks like this shouldn't happen.

onCreated: ->
    Meteor.subscribe 'somethings'
autorun: ->
    Meteor.subscribe 'somethings'
ManuelDeLeon commented 8 years ago

I don't think I can do much since I don't have a mechanism to know when/if you subscribe to a publication.

tomyo commented 8 years ago

I'm using babrahams:constellation, gives you a left panel where you can view the local collections and subscriptions.

ManuelDeLeon commented 8 years ago

I'll implement it if you help me out. The task for you to is "given a function, how do you know the subscriptions it makes (if any) when you run the function?"

You give me that and I'll use it when the template is created and unsubscribe from the publications onDestroyed.

On Sep 2, 2016 1:16 PM, "Tomas Hayes" notifications@github.com wrote:

I'm using babrahams:constellation, gives you a left panel where you can view the local collections and subscriptions.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/270#issuecomment-244464231, or mute the thread https://github.com/notifications/unsubscribe-auth/AED31il-Jq8zU_QgBRf1Y6Q2TNYc8OPPks5qmHYJgaJpZM4J0AHh .

tomyo commented 8 years ago

Ok, I couldn't find that (nice question though), but I found this:

If you call Meteor.subscribe within a reactive computation, for example using Deps.autorun, the subscription will automatically be cancelled when the computation is invalidated or stopped.

I don't know if that helps. In the mean while, how would you recommend to handle subscriptions with viewmodels (when using the pattern of subscribing in the template layer)? Thanks.

ManuelDeLeon commented 8 years ago

Just unsubscribe from the publication onDestroyed.

On Sep 2, 2016 2:01 PM, "Tomas Hayes" notifications@github.com wrote:

Ok, I couldn't find that (nice question though), but I found this http://stackoverflow.com/questions/25995399/iron-router-meteor-auto-unsubscribe :

If you call Meteor.subscribe within a reactive computation, for example using Deps.autorun, the subscription will automatically be cancelled when the computation is invalidated or stopped.

I don't know if that helps. In the mean while, how would you recommend to handle subscriptions with viewmodels (when using the pattern of subscribing in the template layer)? Thanks.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/270#issuecomment-244473910, or mute the thread https://github.com/notifications/unsubscribe-auth/AED31sw8Hog7C0ilWbR068qbXuqDjmN6ks5qmIBkgaJpZM4J0AHh .

tomyo commented 8 years ago

The solution was to use this.templateInstance.subscribe, wich unsubscribes automatically when the template is destroyed. This also gives you the subscriptionsReady helper, available in the template as Template.subscriptionsReady to check if all subscriptions are ready. Cool.

ManuelDeLeon commented 8 years ago

I forgot about that one. Thanks.

On Sep 5, 2016 12:27 PM, "Tomas Hayes" notifications@github.com wrote:

The solution was to use this.templateInstance.subscribe, wich unsubscribes automatically when the template is destroyed. This also gives you the subscriptionsReady helper, available in the template as Template.subscriptionsReady to check if all subscriptions are ready. Cool.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/270#issuecomment-244796277, or mute the thread https://github.com/notifications/unsubscribe-auth/AED31vctCV0A9FMH8kxW1vuP29R3Tabiks5qnF8rgaJpZM4J0AHh .

tomyo commented 8 years ago

Thank you for ViewModel, the reason why I keep using blaze. ^_^

ManuelDeLeon commented 8 years ago

You should try the React version for your next project, it works with or without Meteor =)