0

I am using

Template.taskList.onCreated( () => {
  var projectId = FlowRouter.getParam( 'projectId' );
  var postHooks = {
    before: {
      insert: function ( doc ) {
        doc.projectId = projectId;
        return doc;
      }
    }
  };

  Template.instance().subscribe( 'tasks', projectId );
  AutoForm.addHooks( 'addTask', postHooks );
} );

to add projectId to all created tasks and subscribe to tasks with the specified projectId.

However, when I change the projectId with FlowRouter.setParam( 'projectId' ... ), it seems projectId is not changing in my hook and subscription.

What am I doing wrong? Do I have to put it inside Tracker.autorun() or something? Is it because I use onCreated instead of onRendered?

Jamgreen
  • 10,329
  • 29
  • 113
  • 224

1 Answers1

0

Yes, you indeed need to use autorun. The reason is that flow router parameters are not a reactive datasource, i.e., a change in their value does not trigger reevaluation of code depending on it.

Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
  • How much of the code should be inside autorun? The flow router parameters are reactive when I use populate my template through the helpers, so I do not understand why they are not reactive in this situation. – Jamgreen Nov 08 '15 at 19:59