Meteor-如何从URL参数获取服务器数据?
我试图根据MeteorJS中路由上的URL参数返回不同的数据 在nodejs的背景下,我会这样做:Meteor-如何从URL参数获取服务器数据?,meteor,client-server,single-page-application,iron-router,Meteor,Client Server,Single Page Application,Iron Router,我试图根据MeteorJS中路由上的URL参数返回不同的数据 在nodejs的背景下,我会这样做: 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 ca
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不支持服务器端渲染,所以这是错误的。我仍然在为Meteor的单页客户端渲染绞尽脑汁;在《流星》中我该怎么做
我的第一次尝试(使用IronRouter):
这是我可以用反应变量做的吗?或者从客户机向单独的服务器端点发出AJAX调用?iron router的标准模式是使用route参数订阅数据:
在本例中,
此.params.bar
是路线的唯一参数。它作为参数传递给Meteor.subscribe。服务器可以使用该参数来决定要发布回客户端的内容。waitOn
保证当模板呈现时,数据将在客户端可用。您可以将数据作为第二个参数传递到.render方法
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 });
});
我建议在路由更改时在客户端上获取查询参数。然后使用新参数调用服务器方法。current()是反应式的,所以您可以在自动运行中检查它的参数,并将您的方法调用放入其中。如果我有时间,我将尝试制作一个Meteorpad来演示。对于其他不了解订阅/发布的人,我将Meteor.publish放在我的基本服务器代码中,当调用此订阅函数时,它将被调用!正是我想要的
Router.route('/foo/:bar',{ // Route takes a single parameter
name: 'foo',
waitOn: function(){
return Meteor.subscribe('fooPublication', this.params.bar);
}
});
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 });
});