meteor-space / ui

Pattern-agnostic base UI package to gain control over your Meteor UI
MIT License
121 stars 15 forks source link

How to manage a dynamic subscription based on user input #23

Closed rhyslbw closed 9 years ago

rhyslbw commented 9 years ago

I've currently got a template manager (helpers/events/created callback) with too many concerns, and am looking for advice on how you would implement this using Space UI. This is a custom search feature of Mongo.collection documents that uses a Meteor.subscription inside the template's created callback with ReactiveVars to manage state. I have an autorun wrapping the Meteor.subscribe call so that changes to the ReactiveVar cause the subscription to rerun, and the new data to be published from the server that represents the search results.

In my refactor I plan to have:

What I don't have a plan for is the subscription. My gut feeling is this is something that belongs in the mediator as it's plumbing the store logic into the template. What's been your approach to this?

DominikGuzei commented 9 years ago

Hey @rhyslbw! For subscriptions i mostly use iron:router routes or controllers and construct a URL scheme that mirrors the "user input" as path and/or query params. If its really a non-resource thing that you need to subscribe to, like license data of the current user I tend to put that into a store, depending on the use case. Mediators might be a good place to begin with, but when you realize that you need this data in multiple places, its better to move it into a store :wink:

rhyslbw commented 9 years ago

Thanks for the insight. In the case of a single page app though I think it would provide clarity if there was an example in the Readme of managing the subscription in a store. I'm away from my computer at the moment so I can't test, but does a subscription in the store work the same as if it were just in the template's onRendered callback inside an autorun with a reactive source as an argument?

DominikGuzei commented 9 years ago

Yeah subscriptions work the same way everywhere – they are not reactive by default, but you can make them so by wrapping them in an Tracker.autorun block.

rhyslbw commented 9 years ago

In the case of the TodoMVC example would you put a Meteor.publication for the todos collection? My initial thought was to put it in the TodosController like so:

Dependencies:
  todos: 'Todos'
  meteor: 'Meteor'

onDependenciesReady: ->
  @meteor.publish 'todos', ->
    @todos.find()

… but when the subscription calls in, todos is not defined.

DominikGuzei commented 9 years ago

Hey @rhyslbw I updated the TodoMVC example to include publications and subscriptions :wink: The publication takes a filter parameter to only publish the dataset that is necessary.