Getting data from router in js file

in my Router.js, I get data from the collection:

Router.route(’/farmers/:_id’, {
name: ‘farmersPage’,
data: function() {
return Farmers.findOne(this.params._id);
}
});

And I want to access an attribute in the Farmers element called “joined”, which would be simply {{joined}} in the .html file, but I need to manipulate it in the corresponding js file. How can I get that attribute?

If all your client logic (and “corresponding js file”) runs under the Meteor you can get the data as for Router.data.

var farmerId = Router.current().params._id;
var farmerData = Farmers.findOne(farmerId);

I actually just pass it into the helper, less dependency that way (esp if you want to move away from IR at some point…)

// template (for both helper and event use cases)
.btn(data-farmerId='this._id') {{doSomething this}}
<div class="btn" data-farmerId='this._id'> {{doSomething this}} </div>

//js
Template.blah.helper({
    doSomething: function (templateData) {
//         templateData.joined
    }
});

Template.blah.event({
    'click .btn': function (evt) {
        var farmer = Farmers.findOne($(evt.currentTarget).data('farmerId'));  //<-- minimongo, free
    }
});
1 Like

Sorry I’m just a bit confused as to what the first line does and how it passes data to the helper. Could you elaborate?

I think the first line is Jade which might be confusing if you’re not familiar with it.

In your template:
{{helperName someParameter}}

Whatever you put in place of someParameter will get passed as a parameter to the function defined by the helper.

Your JS:

Template.yourTemplate.helpers({
  helperName: function(parameter){
    //do something with parameter
  })
})

sorry, was too lazy to type it all out, have edited my reply now, so i use the data attribute to pass in the id for events