redress
redress

Reputation: 1439

Session.set() upon route event in Meteor

I have this functionality associated with clicking a button:

 'click .single-speaker-info a': function(ev, speaker){
    ev.preventDefault();
    Session.set('selectedDocId', this._id);
}

But I'd like it to happen upon hitting this route

Router.route('speaker', {
        path:'/speakers/:_id',
        template: 'speaker',
        data: function(){
            return Speakers.findOne(this.params._id);
        },

        //my attempted solution
        selectedDocId: function(){
            Session.set('selectedDocId', this._id);
        }
    });

But I can't find any documentation on how to execute a method on a route.

Here is the Template.helper Im using to get the property Im setting

       Template.speaker.helpers({

            editingDoc: function(){
                return Speakers.findOne({_id: Session.get('selectedDocId')});
            }

        });

Upvotes: 0

Views: 136

Answers (2)

Mirrorcell
Mirrorcell

Reputation: 384

You could use Template.x.rendered, once the page is rendered, any code in that block will be executed. Of course this would not be in your router.

In your case:

Template.speaker.rendered = function() {
    //Get data from router
    var data = Router.current().data();

    Session.set('selectedDocId', data._id);
}

Upvotes: 1

Dan Dascalescu
Dan Dascalescu

Reputation: 151916

But I can't find any documentation on how to execute a method on a route.

Iron Router offers a number of hooks:

  • onRun <-- probably what you need
  • onRerun
  • onBeforeAction
  • onAfterAction
  • onStop

Upvotes: 1

Related Questions