Reputation: 5220
I'm trying to return different data based off a URL parameter on a route in MeteorJS.
From a nodejs background, I would just do this:
testPage = function (req, res, next) {
//Got parameter query from url
console.log('Getting at /testPage/:query '+req.params.query);
//do something with that parameter, say make an HTTP call or query database
var newThing = req.params.query+"hi";
// render the page
var data = {
foo: newThing
};
res.render('testPage', data);
};
Meteor doesn't support server side rendering, so that's out. I'm still wrapping my head around the single-page client rendering of Meteor; how should I go about this in Meteor?
My first attempt (using IronRouter):
Router.route('/testPage/:query', function () {
console.log("Got param at /testPage/:query "+this.params.query);
if(Meteor.isServer){
console.log("This code may never be called?");
//Otherwise, make an HTTP call
// and then somehow pass the data back to the client...
}
this.render("query");
});
Is this something I can do with reactive variables? Or perhaps make an AJAX call from client to separate server endpoint?
Upvotes: 3
Views: 1941
Reputation: 1162
You can pass the data on the .render method as a second argument
Router.route('/testPage/:query', function () {
console.log("Got param at /testPage/:query "+this.params.query);
if(Meteor.isServer){
console.log("This code may never be called?");
//Otherwise, make an HTTP call
// and then somehow pass the data back to the client...
}
this.render("query", {data: queryResult });
});
Upvotes: 0
Reputation: 20227
The canonical pattern for this with iron-router is to subscribe to data using the route parameter(s):
Router.route('/foo/:bar',{ // Route takes a single parameter
name: 'foo',
waitOn: function(){
return Meteor.subscribe('fooPublication', this.params.bar);
}
});
In this example this.params.bar
is the route's only parameter. It is passed as an argument to Meteor.subscribe
. The server can use that parameter in deciding what to publish back to the client. The waitOn
guarantees the data will be available on the client when the template renders.
Upvotes: 2